Re: [mongodb-user] Update field in collection using script

49 views
Skip to first unread message

Scott Hernandez

unread,
Jun 18, 2012, 12:04:22 PM6/18/12
to mongod...@googlegroups.com
Just loop through each document in the collection and replace the
whole array with the new one containing the documents in an update
statement.

On Mon, Jun 18, 2012 at 4:58 PM, Tej Powar <tejp...@gmail.com> wrote:
> Hi
>
> I current have an array in my collection which simple holds strings.
> {"john", "tim", "joe"}
>
> But now i need to change the array so it has mappings e.g. {name = "john",
> date="31/05/2012"}, {name = "tim", date="31/05/2012"} etc.
>
> how can i do i one off script to go through all records in my collection and
> change the format?
>
> its in a pre-prod environment so is ok to do
>
> cheers
>
> --
> 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

Tej Powar

unread,
Jun 18, 2012, 4:36:23 PM6/18/12
to mongod...@googlegroups.com
so would i be able to extract the current data from the array and replace it right?
> mongodb-user+unsubscribe@googlegroups.com

Scott Hernandez

unread,
Jun 18, 2012, 4:52:55 PM6/18/12
to mongod...@googlegroups.com
Yes, you can read just the field which has the array to form the
update statement as well to reduce network overhead.

http://mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields
>> > 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 post to this group, send email to mongod...@googlegroups.com
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com

Tej Powar

unread,
Jun 19, 2012, 7:11:54 AM6/19/12
to mongod...@googlegroups.com
I am creating a a function for my foreach loop but i cannot get the if statement to work

any ideas?

db.funds.find().forEach(function(fund){
            if (fund.obj.options: size: 0)                          
            {
                {printjson(fund.obj.options)}
            }
});

keeps giving me a syntax error stating missing ) after condition
>> > mongodb-user+unsubscribe@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 post to this group, send email to mongod...@googlegroups.com
> To unsubscribe from this group, send email to
> mongodb-user+unsubscribe@googlegroups.com

Scott Hernandez

unread,
Jun 19, 2012, 7:15:06 AM6/19/12
to mongod...@googlegroups.com
This is not valid javascript :)
"if (fund.obj.options: size: 0)"

I think you want "if (fund.obj.options.length == 0)" or something
along those lines.

I'd suggest testing your code with a single document/object to make
sure it works as you expect in the shell before running the loop.
>> >> > 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 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 post to this group, send email to mongod...@googlegroups.com
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com

Tej Powar

unread,
Jun 19, 2012, 8:55:17 AM6/19/12
to mongod...@googlegroups.com
cheers. But now i have a further issue. basically when i run the code it shows the first record ok as the array options contains items but then it states fund.obj has no properties. But i thought the check would have ignored as the rest of the objects do not have the array filled

any ideas?
>> >> > See also the IRC channel -- freenode.net#mongodb
>> >
>> > --
>> > 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
>> > See also the IRC channel -- freenode.net#mongodb
>
> --
> 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

Scott Hernandez

unread,
Jun 19, 2012, 9:00:18 AM6/19/12
to mongod...@googlegroups.com
You need to check each level in the object graph from the start.

So something like :

if(a && a.b && a.b.c && a.b.c.length > 3) {}
>> >> >> > 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 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 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 post to this group, send email to mongod...@googlegroups.com
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com

Sam Millman

unread,
Jun 19, 2012, 9:00:59 AM6/19/12
to mongod...@googlegroups.com
We have no idea how your schema is built so we are staring blind at your code.

When it says it has "no properties" it means that the object is empty or null so when trying to do say, obj.foo it cannot find foo or obj is null and it throws an error.

I would double check your JS function there.

Tej Powar

unread,
Jun 19, 2012, 9:31:25 AM6/19/12
to mongod...@googlegroups.com
hey 

got the following code now but need to update the array item not sure if this would be the correct way of doing it

db.funds.find({'obj.options':{ $size : 5}}).forEach(function(fund){
var newItem = {};

if (fund.obj.options != null && fund.obj.options.length > 0)
{   
for(var i = 0; i < fund.obj.options.length; i++)
{
   var original = fund.obj.options[i];
   fund.obj.options[i] = {name: original, dateOfApproval: null};
           
}
}
});

cheers
>> >> >> > See also the IRC channel -- freenode.net#mongodb
>> >> >
>> >> > --
>> >> > 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
>> >> > See also the IRC channel -- freenode.net#mongodb
>> >
>> > --
>> > 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
>> > See also the IRC channel -- freenode.net#mongodb
>
> --
> 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

Jeremy Mikola

unread,
Jul 10, 2012, 3:31:50 PM7/10/12
to mongod...@googlegroups.com

On Tuesday, June 19, 2012 9:31:25 AM UTC-4, Tej Powar wrote:

got the following code now but need to update the array item not sure if this would be the correct way of doing it

Within your forEach callback, you can pass the fund variable to the db.funds.save() method, as documented in MongoDB Shell: Updating. The save() method essentially checks whether the document has an "_id" field and performs an insert() or update() accordingly.

Alternatively, you may want to do a single update operation to set the "obj.options" field. This is documented in Updating: $set.

Keep in mind that since you're updating records within a collection during iteration, you should consider snapshotting the cursor. To quote the docs:

snapshot() mode assures that objects which update during the lifetime of a query are returned once and only once. This is most important when doing a find-and-update loop that changes the size of documents that are returned ($inc does not change size).

Reply all
Reply to author
Forward
0 new messages