Positional operator with multiple levels of nesting. Options

1,307 views
Skip to first unread message

Antoine

unread,
Jul 12, 2010, 5:48:59 AM7/12/10
to mongodb-user
Dear All,

I would like to know if it is possible to update a fied located in
multiple levels of nesting even if this feature is not implemented :

http://jira.mongodb.org/browse/SERVER-831

Is there another way to acheive this goal which work with mongodb ?

Thank you !

Antoine

Antoine

unread,
Jul 12, 2010, 9:26:12 AM7/12/10
to mongodb-user
any help ?

Thank you !

Antoine

Eliot Horowitz

unread,
Jul 12, 2010, 9:28:06 AM7/12/10
to mongod...@googlegroups.com
Can you provide an example

> --
> 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.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

Antoine

unread,
Jul 12, 2010, 9:34:19 AM7/12/10
to mongodb-user
yes of course

I have this document

{
"_id": "4c39db74fb4e1c0177000009",
"translations": [
{
"player_ids": [
"4c39db74fb4e1c0177000565",
"4c39db74fb4e1c0177000007"
],
"_id": "4c39db74fb4e1c017700000a"
},
{
"player_ids": [
"4c39db74fb4e1c0177000005",
"4c39db74fb4e1c0177000677"
],
"_id": "4c39db74fb4e1c017700000a"
}

]
}

I woud like to update this document and replace
"4c39db74fb4e1c0177000677" by "xxxxxxxxxxxxxxxxxxxxxx" in the array
field "player_ids"

How to write this query ?

Thank you so much for your help

Antoine

On Jul 12, 3:28 pm, Eliot Horowitz <eliothorow...@gmail.com> wrote:
> Can you provide an example
>

Antoine

unread,
Jul 12, 2010, 9:38:19 AM7/12/10
to mongodb-user
Just a precision, I have not just one document bu plenty of documents
and I would like to update all the documents in which the array field
"player_ids" contains 4c39db74fb4e1c0177000677 to replace by another
value ... ;-)

Michael Dirolf

unread,
Jul 12, 2010, 10:02:21 AM7/12/10
to mongod...@googlegroups.com
I don't think the current update syntax supports doing that directly.
You could do it in two steps by first adding the new string where you
find the old one, and then removing the old one.

Antoine

unread,
Jul 12, 2010, 10:21:02 AM7/12/10
to mongodb-user
Ok. What are the two step query to perform ?

Thank you for you help !

On Jul 12, 4:02 pm, Michael Dirolf <m...@10gen.com> wrote:
> I don't think the current update syntax supports doing that directly.
> You could do it in two steps by first adding the new string where you
> find the old one, and then removing the old one.
>

Michael Dirolf

unread,
Jul 12, 2010, 10:34:20 AM7/12/10
to mongod...@googlegroups.com
Do a $push to add the new one, and then an $pull to remove the old one.

http://www.mongodb.org/display/DOCS/Updating

Antoine

unread,
Jul 12, 2010, 11:23:16 AM7/12/10
to mongodb-user
But the order of each element in the array is not respect ?

On Jul 12, 4:34 pm, Michael Dirolf <m...@10gen.com> wrote:
> Do a $push to add the new one, and then an $pull to remove the old one.
>
> http://www.mongodb.org/display/DOCS/Updating
>

Michael Dirolf

unread,
Jul 12, 2010, 11:29:09 AM7/12/10
to mongod...@googlegroups.com
Right. If you need to maintain order you can try a $set w/ the
postional ($) operator. But it might be a bit trickier to get right,
for multiple updates. Here's an example:

> db.test.drop();
false
> db.test.save({x: [1, 2, 3, 4]});
> db.test.update({x: 2}, {$set: {"x.$": 5}});
> db.test.find();
{ "_id" : ObjectId("4c3b345b9b7b0b1a73efe440"), "x" : [ 1, 5, 3, 4 ] }

Antoine

unread,
Jul 12, 2010, 5:36:33 PM7/12/10
to mongodb-user
Does it work if the array is in a deep level tree like my example :

{
"_id": "4c39db74fb4e1c0177000009",
"translations": [
{
"player_ids": [
"4c39db74fb4e1c0177000565",
"4c39db74fb4e1c0177000007"
],
"_id": "4c39db74fb4e1c017700000a"
},
{
"player_ids": [
"4c39db74fb4e1c0177000005",
"4c39db74fb4e1c0177000677"
],
"_id": "4c39db74fb4e1c01770000bb"
}

]

}


On Jul 12, 5:29 pm, Michael Dirolf <m...@10gen.com> wrote:
> Right. If you need to maintain order you can try a $set w/ the
> postional ($) operator. But it might be a bit trickier to get right,
> for multiple updates. Here's an example:
>
> > db.test.drop();
> false
> > db.test.save({x: [1, 2, 3, 4]});
> > db.test.update({x: 2}, {$set: {"x.$": 5}});
> > db.test.find();
>
> { "_id" : ObjectId("4c3b345b9b7b0b1a73efe440"), "x" : [ 1, 5, 3, 4 ] }
>

Michael Dirolf

unread,
Jul 12, 2010, 5:37:48 PM7/12/10
to mongod...@googlegroups.com
Yes it should - you can always try running from the shell yourself, too.

Antoine

unread,
Jul 13, 2010, 1:19:21 PM7/13/10
to mongodb-user
I don't really understand .. I tried :

db.test.save({x: [1,2,{y: [1, 2, 3, 4]}]});
I woud like to replace 4 to 45 so I write :

db.test.update({"x.y": 4}, {$set: {"x.y.$": 45}});

it is not working.

any help ?

Thank you very much ;-)

Antoine

On Jul 12, 11:37 pm, Michael Dirolf <m...@10gen.com> wrote:
> Yes it should - you can always try running from the shell yourself, too.
>

Michael Dirolf

unread,
Jul 13, 2010, 1:22:29 PM7/13/10
to mongod...@googlegroups.com
Sorry I thought you meant an array w/in nested documents, not other
nested arrays. For your scenario you'd need this case to be
implemented:
http://jira.mongodb.org/browse/SERVER-831

Antoine

unread,
Jul 13, 2010, 1:39:29 PM7/13/10
to mongodb-user
Ok I was thinking I can use mongodb but I need to update some array in
some nested document..... !

I hope I will be able to do that soon ;-)

Thank you for your answer ;-)

Antoine

On Jul 13, 7:22 pm, Michael Dirolf <m...@10gen.com> wrote:
> Sorry I thought you meant an array w/in nested documents, not other
> nested arrays. For your scenario you'd need this case to be
> implemented:http://jira.mongodb.org/browse/SERVER-831
>

Michael Dirolf

unread,
Jul 13, 2010, 1:44:40 PM7/13/10
to mongod...@googlegroups.com
Often it's possible to rethink your schema to get around the need for
these sorts of complex updates. Might be worth reading some of the
example schemas posted on various blogs, the mongodb cookbook, etc. to
get some ideas.

Antoine

unread,
Jul 13, 2010, 1:56:16 PM7/13/10
to mongodb-user
I don't fell it is really complex operation to update a nested array
field ?

I don't understand why we shoud use nested document if we can't update
them ;-)

Thank you again ;-)

Antoine

On Jul 13, 7:44 pm, Michael Dirolf <m...@10gen.com> wrote:
> Often it's possible to rethink your schema to get around the need for
> these sorts of complex updates. Might be worth reading some of the
> example schemas posted on various blogs, the mongodb cookbook, etc. to
> get some ideas.
>

Michael Dirolf

unread,
Jul 13, 2010, 2:29:48 PM7/13/10
to mongod...@googlegroups.com
You can update nested arrays in the current version by using array
indexes in your updates, but if you want to update based on a query
using the positional operator you can only update a value w/in a
single array, not nested in multiple arrays.

That limitation is there mainly because the positional operator is
pretty new. See the case I mentioned above which will add support for
this.

Antoine

unread,
Jul 13, 2010, 2:49:57 PM7/13/10
to mongodb-user
array indexes in your updates ?
How should I write the query if i have :

db.test.save({x: [1,2,{y: [1, 2, 3, 4]}]});

I woud like to replace 4 to 45 so how should I write ?
.... ?

Thank you so much ;-)
Antoine

On Jul 13, 8:29 pm, Michael Dirolf <m...@10gen.com> wrote:
> You can update nested arrays in the current version by using array
> indexes in your updates, but if you want to update based on a query
> using the positional operator you can only update a value w/in a
> single array, not nested in multiple arrays.
>
> That limitation is there mainly because the positional operator is
> pretty new. See the case I mentioned above which will add support for
> this.
>
> ...
>
> read more »

Michael Dirolf

unread,
Jul 13, 2010, 2:52:58 PM7/13/10
to mongod...@googlegroups.com
From the shell:

> db.test.save({x: [1, 2, {y: [1, 2, 3, 4]}]});
> db.test.update({}, {"$set": {"x.2.y.3": 45}});
> db.test.find()
{ "_id" : ObjectId("4c3cb5e40763d13a06a2fa58"), "x" : [ 1, 2, { "y" :
[ 1, 2, 3, 45 ] } ] }

Antoine

unread,
Jul 13, 2010, 2:58:27 PM7/13/10
to mongodb-user
Ok the things is I don't know before the query where is the value 4.

So it's impossible to write : "x.2.y.3"

I don't know what is the index of the array matching the value 4..

The problem is still here ... !

Anyway, Thank you for help ;-)

Antoine

On Jul 13, 8:52 pm, Michael Dirolf <m...@10gen.com> wrote:
> From the shell:
>
> > db.test.save({x: [1, 2, {y: [1, 2, 3, 4]}]});
> > db.test.update({}, {"$set": {"x.2.y.3": 45}});
> > db.test.find()
>
> { "_id" : ObjectId("4c3cb5e40763d13a06a2fa58"), "x" : [ 1, 2, { "y" :
> [ 1, 2, 3, 45 ] } ] }
>
> ...
>
> read more »

Antoine

unread,
Jul 14, 2010, 5:29:12 AM7/14/10
to mongodb-user
Sorry to bother you Michael, but don't you think it's a priority task
to be able to update a fied in a nested arrays of a document ?

On Jul 13, 8:58 pm, Antoine <antoine.fauc...@gmail.com> wrote:
> Ok the things is I don't know before the query where is the value 4.
>
> So it's impossible to write : "x.2.y.3"
>
> I don't know what  is the index of the array matching the value 4..
>
> The problem is still here ... !
>
> Anyway, Thank you for help ;-)
>
> Antoine
>
> On Jul 13, 8:52 pm, Michael Dirolf <m...@10gen.com> wrote:
>
> > From the shell:
>
> > > db.test.save({x: [1, 2, {y: [1, 2, 3, 4]}]});
> > > db.test.update({}, {"$set": {"x.2.y.3": 45}});
> > > db.test.find()
>
> > { "_id" : ObjectId("4c3cb5e40763d13a06a2fa58"), "x" : [ 1, 2, { "y" :
> > [ 1, 2, 3, 45 ] } ] }
>
> > On Tue, Jul 13, 2010 at 2:49 PM, Antoine <antoine.fauc...@gmail.com> wrote:
> > > array indexes in your updates ?
> > > How should I write the query if i have :
>
> > > db.test.save({x: [1,2,{y: [1, 2, 3, 4]}]});
>
> > > I woud like to replace 4 to 45 so how should I write  ?
> > > .... ?
>
> > > Thank you so much ;-)
> > > Antoine
>
> > > On Jul 13, 8:29 pm, Michael Dirolf <m...@10gen.com> wrote:
> > >> You can updatenestedarrays in the current version by using array
> > >> indexes in your updates, but if you want to update based on a query
> > >> using the positional operator you can only update a value w/in a
> > >> single array, notnestedin multiple arrays.
>
> > >> That limitation is there mainly because the positional operator is
> > >> pretty new. See the case I mentioned above which will add support for
> > >> this.
>
> > >> On Tue, Jul 13, 2010 at 1:56 PM, Antoine <antoine.fauc...@gmail.com> wrote:
> > >> > I don't fell it is really complex operation to update anestedarray
> > >> > field ?
>
> > >> > I don't understand why we shoud usenesteddocument if we can't update
> > >> > them ;-)
>
> > >> > Thank you again ;-)
>
> > >> > Antoine
>
> > >> > On Jul 13, 7:44 pm, Michael Dirolf <m...@10gen.com> wrote:
> > >> >> Often it's possible to rethink your schema to get around the need for
> > >> >> these sorts of complex updates. Might be worth reading some of the
> > >> >> example schemas posted on various blogs, the mongodb cookbook, etc. to
> > >> >> get some ideas.
>
> > >> >> On Tue, Jul 13, 2010 at 1:39 PM, Antoine <antoine.fauc...@gmail.com> wrote:
> > >> >> > Ok I was thinking I can use mongodb but I need to update some array in
> > >> >> > somenesteddocument..... !
>
> > >> >> > I hope I will be able to do that soon ;-)
>
> > >> >> > Thank you for your answer ;-)
>
> > >> >> > Antoine
>
> > >> >> > On Jul 13, 7:22 pm, Michael Dirolf <m...@10gen.com> wrote:
> > >> >> >> Sorry I thought you meant an array w/innesteddocuments, not other
> > >> >> >>nestedarrays. For your scenario you'd need this case to be
> ...
>
> read more »

Michael Dirolf

unread,
Jul 14, 2010, 11:05:53 AM7/14/10
to mongod...@googlegroups.com
Again, you already can update w/in a nested array - just not using the
positional operator. Support for that is scheduled for 1.7, as you can
see on the case I linked you too.

I think it's important and we will work on it, but we are all very
focused on 1.6 right now :).

Reply all
Reply to author
Forward
0 new messages