Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

mongodb - Change all my documents nested document value

In mongos shell how would I go through and change every document in reviews.category to "category 2"

My Documents Structure:

{
   "_id": ObjectId("4fb3f443b1445d24fc000000"),
   "reviews": {
     "0": {
       "category": "category 1"
    },
     "1": {
       "category": "category 1"
    },
     "2": {
       "category": "category 1"
    },
     "3": {
       "category": "category 1"

    } 
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You will have to do this yourself in your application code, by querying the document, and looping over all of your nested documents; and then save it back to MongoDB.

In order to prevent race conditions with this, please have a look at the section compare and swap at http://www.mongodb.org/display/DOCS/Atomic+Operations

There is currently an open ticket for this to add this functionality to MongoDB. You might want to up-vote it: https://jira.mongodb.org/browse/SERVER-1243


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...