Updating using modifier operators in the Mongo CLI works, but not from node.js code using mongoose

Connecting to our Mongo instance using the CLI, you can use the update modifier operators just fine:

db.users.update ({nickname: 'mcoalson'}, {"$ addToSet": {room_ref: "b"}}) db.users.update ({nickname: 'mcoalson'}, {"$ addToSet": { room_ref: "c"}}) db.users.findOne ({nickname: 'mcoalson'}) {"_id": ObjectId ("4de5e9e982e9556c2a000003"), "nickname": "mcoalson", "room_ref": ["A" , "D", "B", "C"]}

However, doing the same in node.js in the same document, there are no results.

db.User.update({'nickname': 'mcoalson'}, {"$pullAll": {'room_ref': ["b"]}});
db.User.update({'nickname': 'mcoalson'}, {"$addToSet": {'room_ref': "f"}});

"room_ref": ["A", "D", "B", "C"]

Clearly, “b” was not deleted, and “f” was not added. I tried all conceivable quotation scenarios that I could think of, but nothing changes. Can I use find () and findOne () from node.js code and am I using the same credentials, any tips?

+5
source share
2 answers

Sorry for delay.

Mongo CLI synchronously, node.js is asynchronous. Do you use the code at the time of publication, or do you have the appropriate callbacks? Your test should look like this:

db.User.update({'nickname': 'mcoalson'}, {"$pullAll": {'room_ref': ["b"]}}, function(){
    db.User.findOne({'nickname': 'mcoalson'}, function(user){
        console.log('changed user:', user)
    })
})
0

, , .

,

db.User.update(...)

:

db.User.collection.update(...)

, .

0

All Articles