1

I'm quite good at SQL, but I'm struggling with even the simplest Mongo queries.

My documents in the post collection looks like this (simplified)

{
 '_id': 5
 'body': 'Correct horse battery staple',
 'comments': 
 [{
 user_id: '123'
 }, 
 {
 user_id: '456'
 }, 
 {
 user_id: '123'
 }]
}

I need to update the user_id's that are '123' to 'abc' for all posts.

In a SQL database, I would have a post table and a comment table and do this:

UPDATE comment SET user_id = 'abc' WHERE user_id = '123'
Amarnath Krishnan
1,2631 gold badge9 silver badges12 bronze badges
asked Dec 18, 2013 at 21:33
1

3 Answers 3

1

I think you're looking for FindAndModify:

db.runCommand({
 findAndModify: "post",
 query: { user_id: 123 },
 update: { $set: { user_id: 'abc' } }
})

Edit

I believe you can do a similar thing on a collection with update if multi is set to true:

db.post.update(
 { user_id: 123 },
 { $set: { user_id: 'abc' } },
 { multi: true }
)
answered Dec 18, 2013 at 22:24
Sign up to request clarification or add additional context in comments.

1 Comment

I need to modify all documents, not just one. I updated my question to be more explicit.
1

By default, mongodb update command will update only one document.

db.collection.update({document to be found},{changes to be done})

To update multiple document, you should include multi keyword.

db.collection.update({document to be found},{changes to be done},{multi:true})

Assuming your document structure as below:

{
 "_id": 5,
 "body": "Correct horse battery staple",
 "comments": [{"user_id": "123"},{"user_id": "456"},{"user_id": "123"}]
}
{
 "_id": 6,
 "body": "Correct horse battery staple",
 "comments": [{"user_id': "23"},{"user_id": "123"},{"user_id": "13"}]
}

In this case, i may need to update multiple elements inside an array as well as multiple documents. There is no default mongodb query to do it. Instead i will loop through documents and do it as follows.

// loop until all documents has been updated
while(db.post.find({'comments.user_id':'123'}).count()!=0) 
{
 db.post.update(
 { 'comments.user_id':'123' },
 { $set:{'comments.$.user_id':'abc'} },
 { multi:true }
 )
}

After the 1st loop run, post collection will look like:

{
 "_id": 5,
 "body": "Correct horse battery staple",
 "comments": [{"user_id": "abc"},{"user_id": "456"},{"user_id": "123"}]
}
{
 "_id": 6,
 "body": "Correct horse battery staple",
 "comments": [{"user_id": "23"},{"user_id": "abc"},{"user_id": "13"}]
}

After the 2nd loop run, post collection will look like:

{
 "_id": 5,
 "body": "Correct horse battery staple",
 "comments": [{"user_id": "abc"},{"user_id": "456"},{"user_id": "abc"}]
}
{
 "_id": 6,
 "body": "Correct horse battery staple",
 "comments": [{"user_id": "23"},{"user_id": "abc"},{"user_id": "13"}]
}

In the third loop run, the loop gets terminated.

answered Dec 19, 2013 at 5:35

Comments

0

You can use arrayFilters from v3.6,

db.post.updateMany(
 { 'comments.user_id': '123' },
 { 
 $set: {
 'comments.$[elem].user_id': 'abc' 
 }
 },
 { 
 arrayFilters: [
 { 'elem.user_id': '123' }
 ]
 }
);
answered Aug 13, 2020 at 9:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.