Easy way to add field in array of objects

31 views
Skip to first unread message

Arthur Almeida Pereira

unread,
May 24, 2013, 9:37:35 AM5/24/13
to mongod...@googlegroups.com
Hello, 

  This is an example of document:

  { 
    name : "abc",
    values : [
      { size : 1, height : 10 }, { size : 2, height : 12 }, { size : 3, height : 30 }
    ]
  }

  I want to add the field "width" to all objects inside the array values.
  I created a script that iterates over the collection and add the field in each object in array and update each document.
  There is another way to do that? With a simple db.collection.update() ?

cheers,  

Tyler Brock

unread,
May 24, 2013, 12:27:59 PM5/24/13
to mongod...@googlegroups.com
You would have to issue an update for the entire array. Assuming you would want to add the width field to all of the items in the array and that the width is not the same for each of them one update would not suffice regardless of how you do it.

If, however you can write a query to select the item you would like to update you can use the positional operator to add ($set) the width of the item.

-Tyler

Gary R

unread,
May 24, 2013, 12:46:32 PM5/24/13
to mongod...@googlegroups.com
I might add to Tyler's comment that (even if the width values were all the same) I don't believe it is yet possible to use an update with the positional operator to iterate through the documents in your array.  This does appear to be a requested feature.

Gary R.

Arthur Cláudio Almeida Pereira

unread,
May 24, 2013, 9:03:42 PM5/24/13
to mongod...@googlegroups.com
Hi!

  Thanks for the replies!
  I just wanted to know if was possible do it without update each document. 

cheers, 


2013/5/24 Gary R <grech...@gmail.com>
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages