Rename embedded field

1,094 views
Skip to first unread message

alex

unread,
Feb 9, 2011, 3:29:22 AM2/9/11
to mongodb-user
I'm using Mongo 1.7.5 and its new $rename function.

db.Users.update({}, {$rename:{"name":"Name"}}, true, true)

But how can I make it work with embedded documents?

db.Users.update({}, {$rename:{"Address.street":"Address.Street"}},
true, true)

doesn't work

Thank you

Andreas Jung

unread,
Feb 9, 2011, 3:43:34 AM2/9/11
to mongod...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Working for me as it should:

> db.foo.insert({foo : {bar : 2}})
> db.foo.find()
{ "_id" : ObjectId("4d5261c0774936147acbc8ab"), "foo" : { "bar" : 2 } }
> db.foo.update({}, {$rename:{'foo.bar' : 'FOO.BAR'}})
> db.foo.find()
{ "FOO" : { "BAR" : 2 }, "_id" : ObjectId("4d5261c0774936147acbc8ab"),
"foo" : { } }


- -aj
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQGUBAEBAgAGBQJNUlO1AAoJEADcfz7u4AZjGs8Lv02W2h/6hgY5OO23gKBwiCwr
gdRzOy7veoEaL0O5ZUDvLkEPG6tAAzQxjMo4T1UGnDIoYJTlHdk9nEw8h9O5aXhp
fyPLlQzuRbvp+LZgTpqLh9vDuhauvMaBIPLMXRS1Jd6yvqAYRkkq1DG5DRdGSI79
CY6oDOqef4zQ5k4YpDPNlSKcsSDK+konbC18Nkd6RxkoXi38avK2kAciRf71v1VG
CDFc2TBLOj6PIM74TjaEFH31UXmgP5B4iYZYPLx53d9kZWsEL7Dyu8rxbtDVku/i
SYKZJjzbpf2e1Kc8xCwJr9MuIU9t+aIUy28I6+4FQgrR/l5q7wZ1nZFJU4SmmEah
oeQeReRfULmOTymbDGiDDwsGRjMhsl/Ala28KefI+aS5hzXlJKUXyHeFU2qD+eH2
XYIA/hI8f3pTfDyEvdKJOjvbc8y/e1EMhZHRlHzf4kQ2gSdajKUxmwB8zfNTBqED
9Oy+DBXM4gxuyqntmjmudbxFGKrcBy8=
=AKFx
-----END PGP SIGNATURE-----

lists.vcf

alex

unread,
Feb 9, 2011, 3:45:21 AM2/9/11
to mongodb-user
Thank you for your reply.

My bad. I forgot to mention that I have an array of embedded
documents. Is there a way to rename that?

On Feb 9, 11:43 am, Andreas Jung <li...@zopyx.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> alex wrote:
> > I'm using Mongo 1.7.5 and its new $rename function.
>
> > db.Users.update({}, {$rename:{"name":"Name"}}, true, true)
>
> > But how can I make it work with embedded documents?
>
> > db.Users.update({}, {$rename:{"Address.street":"Address.Street"}},
> > true, true)
>
> Working for me as it should:
>
> > db.foo.insert({foo : {bar : 2}})
> > db.foo.find()
>
> { "_id" : ObjectId("4d5261c0774936147acbc8ab"), "foo" : { "bar" : 2 } }> db.foo.update({}, {$rename:{'foo.bar' : 'FOO.BAR'}})
> > db.foo.find()
>
> { "FOO" : { "BAR" : 2 }, "_id" : ObjectId("4d5261c0774936147acbc8ab"),
> "foo" : { } }
>
> - -aj
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.11 (Darwin)
> Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org/
>
> iQGUBAEBAgAGBQJNUlO1AAoJEADcfz7u4AZjGs8Lv02W2h/6hgY5OO23gKBwiCwr
> gdRzOy7veoEaL0O5ZUDvLkEPG6tAAzQxjMo4T1UGnDIoYJTlHdk9nEw8h9O5aXhp
> fyPLlQzuRbvp+LZgTpqLh9vDuhauvMaBIPLMXRS1Jd6yvqAYRkkq1DG5DRdGSI79
> CY6oDOqef4zQ5k4YpDPNlSKcsSDK+konbC18Nkd6RxkoXi38avK2kAciRf71v1VG
> CDFc2TBLOj6PIM74TjaEFH31UXmgP5B4iYZYPLx53d9kZWsEL7Dyu8rxbtDVku/i
> SYKZJjzbpf2e1Kc8xCwJr9MuIU9t+aIUy28I6+4FQgrR/l5q7wZ1nZFJU4SmmEah
> oeQeReRfULmOTymbDGiDDwsGRjMhsl/Ala28KefI+aS5hzXlJKUXyHeFU2qD+eH2
> XYIA/hI8f3pTfDyEvdKJOjvbc8y/e1EMhZHRlHzf4kQ2gSdajKUxmwB8zfNTBqED
> 9Oy+DBXM4gxuyqntmjmudbxFGKrcBy8=
> =AKFx
> -----END PGP SIGNATURE-----
>
>  lists.vcf
> < 1KViewDownload

Nat

unread,
Feb 9, 2011, 9:32:55 AM2/9/11
to mongodb-user
I don't think it supports that at the moment.

Eliot Horowitz

unread,
Feb 9, 2011, 11:11:01 PM2/9/11
to mongod...@googlegroups.com
It only works for embedded docs, not all values in an array.
You can open a jira if you'ld like that feature.

> --
> 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.
>
>

Mike O'Brien

unread,
Jun 11, 2012, 2:09:16 PM6/11/12
to mongodb-user
The easiest way to do this is probably to perform a "$set" operation
where you overwrite the entire array with the values in the new
format. You will need to do the re-naming on the client side first.

On Jun 3, 10:57 pm, Michael Yagudaev <michaelyagud...@gmail.com>
wrote:
> Is this still the case today? If so, what is the recommended way of doing
> this type of rename operation?
>
> I thought of removing and inserting the modified record. Or is there a
> better approach?
>
>
>
>
>
>
>
> On Wednesday, February 9, 2011 10:11:01 PM UTC-6, Eliot wrote:
>
> > It only works for embedded docs, not all values in an array.
> > You can open a jira if you'ld like that feature.
>

Michael Yagudaev

unread,
Jun 13, 2012, 12:46:39 PM6/13/12
to mongod...@googlegroups.com
That's not a bad idea. Is it also faster in terms of performance?

See also the IRC channel -- freenode.net#mongodb



--
Michael Yagudaev
Software/Web Developer
Twitter: @yagudaev

"Think like a man of action, act like a man of thought" --  Henri Louis Bergson

Mike O'Brien

unread,
Jun 13, 2012, 1:07:58 PM6/13/12
to mongodb-user
Performance will depend somewhat on how big the array is and much the
size of the object changes (if it triggers document moves) but in
general it should be quite fast.

On Jun 13, 12:46 pm, Michael Yagudaev <michaelyagud...@gmail.com>
> *Michael Yagudaev
> Software/Web Developer
> Phone: 204-891-2491
> Email: mich...@yagudaev.com
> Website:www.yagudaev.com
> Twitter: @yagudaev <http://twitter.com/#!/yagudaev>
>
> "Think like a man of action, act like a man of thought" --  Henri Louis
> Bergson
> *

The JMCNet Team

unread,
Aug 22, 2013, 12:15:50 PM8/22/13
to mongod...@googlegroups.com
Sorry to bring this old post to life again, but I wonder if this feature (renaming a field of an embedded object in an array) is still not developped, and if not, what is the best way to simulate such an operation in one query (or two). I guess with the aggregation we can get a modified object and then update the original one, but I couldn't find how to do simply
What I want to do is to rename : "budgetted" into "planned" on such object :
> db.doc.find().pretty()
{
        "_id" : "521633918DA674FDD1B2AFE4",
        "charges" : [
                {
                        "label" : "The label",
                        "budgetted" : 14,
                },
                {
                        "label" : "The label2",
                        "budgetted" : 15,
                },
        ],
}

Thanks in advance for your answer.

JM.

The JMCNet Team

unread,
Sep 6, 2013, 8:17:45 AM9/6/13
to mongod...@googlegroups.com
No idea ?
Reply all
Reply to author
Forward
0 new messages