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?

Community
  • 1
  • 1
user1460015
  • 1,973
  • 3
  • 27
  • 37

2 Answers2

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.

Miguel Cartagena
  • 2,576
  • 17
  • 20
  • 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? – user1460015 Dec 21 '12 at 14:23
  • 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. – Miguel Cartagena Dec 26 '12 at 10:00
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.

Dmitry Zagorulkin
  • 8,370
  • 4
  • 37
  • 60
  • Adding a new key must be done through a script and not through the mongo shell? – user1460015 Dec 21 '12 at 17:10
  • If you can do it through mongo shell that means you can use it through each mongo driver. – Dmitry Zagorulkin Dec 21 '12 at 18:11
  • 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. – user1460015 Dec 21 '12 at 18:54
  • it's just example. i had want to get you a point for solving your problem. http://api.mongodb.org/python/current/examples/aggregation.html – Dmitry Zagorulkin Dec 21 '12 at 19:32
  • 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. – user1460015 Dec 21 '12 at 20:12
  • You may solve be use mongo shell directly. What you did't understood? – Dmitry Zagorulkin Dec 22 '12 at 07:59