4

Similar to this question

Barrowing the data set, I have something similar to this:

{
    'user_id':'{1231mjnD-32JIjn-3213}',
    'name':'John',
    'campaigns':
        [
            {
                'campaign_id':3221,
                'start_date':'12-01-2012',
            },
            {
                'campaign_id':3222,
                'start_date':'13-01-2012',
            }
        ]
}

And I want to add a new key in the campaigns like so:

{
    'user_id':'{1231mjnD-32JIjn-3213}',
    'name':'John',
    'campaigns':
        [
            {
                'campaign_id':3221,
                'start_date':'12-01-2012',
                'worker_id': '00000'
            },
            {
                'campaign_id':3222,
                'start_date':'13-01-2012',
                'worker_id': '00000'
            }
        ]
}

How to insert/update a new key into an array of objects?
I want to add a new key into every object inside the array with a default value of 00000.

I have tried:
db.test.update({}, {$set: {'campaigns.worker_id': 00000}}, true, true)
db.test.update({}, {$set: {campaigns: {worker_id': 00000}}}, true, true)

Any suggestions?

2 Answers 2

2

I'm supposing that this operation will occur once, so you can use a script to handle it:

var docs = db.test.find();
for(var i in docs) {
    var document = docs[i];

    for(var j in document.campaigns) {
        var campaign = document.campaigns[j];
        campaign.worker_id = '00000';
    }

    db.test.save(document);
}

The script will iterate over all documents in your collection then over all campaigns in each document, setting the *worker_id* property. At the end, each document is persisted.

Sign up to request clarification or add additional context in comments.

2 Comments

I put the code into a file called addTo.js. Then in the mongo shell I have mongo addTo.js. The error follows: InternalError: too much recursion src/mongo/shell/collection.js:131. Is this the correct way?
How much documents you have in this collection? Maybe you should run the script many times, using skip and limit function to avoid this error.
1
db.test.update({}, {$set: {'campaigns.0.worker_id': 00000}}, true, true

this will update 0 element.

if you want to add a new key into every object inside the array you should use: $unwind

example:

{
  title : "this is my title" ,
  author : "bob" ,
  posted : new Date() ,
  pageViews : 5 ,
  tags : [ "fun" , "good" , "fun" ] ,
  comments : [
      { author :"joe" , text : "this is cool" } ,
      { author :"sam" , text : "this is bad" }
  ],
  other : { foo : 5 }
}

unwinding tags

db.article.aggregate(
    { $project : {
        author : 1 ,
        title : 1 ,
        tags : 1
    }},
    { $unwind : "$tags" }
);

result:

{
     "result" : [
             {
                     "_id" : ObjectId("4e6e4ef557b77501a49233f6"),
                     "title" : "this is my title",
                     "author" : "bob",
                     "tags" : "fun"
             },
             {
                     "_id" : ObjectId("4e6e4ef557b77501a49233f6"),
                     "title" : "this is my title",
                     "author" : "bob",
                     "tags" : "good"
             },
             {
                     "_id" : ObjectId("4e6e4ef557b77501a49233f6"),
                     "title" : "this is my title",
                     "author" : "bob",
                     "tags" : "fun"
             }
     ],
     "OK" : 1
}

After you could write simple updaiting query.

6 Comments

Adding a new key must be done through a script and not through the mongo shell?
If you can do it through mongo shell that means you can use it through each mongo driver.
Can you do it through the mongo shell? As far as I know aggregate doesn't modify the database but outputs a document that is requested. I want to modify the actual db.
it's just example. i had want to get you a point for solving your problem. api.mongodb.org/python/current/examples/aggregation.html
I understand and thank you but I was looking for something that modifies the document through the mongo shell. I am sure this problem is solvable applying a script but I would like to use the mongo shell directly.
|

Your Answer

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.