I want to do simple database update using _MONGOSH in Mongo Compass. I am putting this query, and getting error: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer;
My query:
db.getCollection("col")
.updateMany(
{
field: { $ne: null }
},
{
field: ObjectId('$field')
}
)
This is how I solved this:
db.getCollection("col").find(
{
'field': { $type : 2 },
'field': { $ne: null }
})
.forEach(async function(d){
const id = ObjectId(d.field);
d.field = id;
await db.getCollection("col").updateOne(
{
_id: d._id
},
{
$set: {
'field': id
}
}
)
}
)