I have tried to pass the entire collection, and if it saves the value, as an array (logical), but what I want is that it only saves the newField and secondField fields with their respective values in the old_user collection of the new_user collection, but I save them as null, this is the code:
db.getCollection("old_user").find()
.forEach((elm) => {
let elmId = elm.id
let new_user = db.getCollection("new_user").find( {id : elmId}).toArray()
if(new_user) {
db.getCollection("old_user").updateOne({id: elmId}, {$set: {newfield: new_user.newField, secondfield: new_user.secondField}})
}
})
As far as I know, to access an object inside an array it is with the point annotation, if I make a query of the entire collection it returns it to me but if I do a console.log of new_user.newField it returns it to me as null
I appreciate any help!
The problem is that you are using
find
And
find
it returns multiple elements. Therefore the propertynew_user.newField
does not exist.You must change it to
findOne
: