removing multiple fields from the same document ($unsetall?)

1,516 views
Skip to first unread message

Laszlo Hornyak

unread,
Jul 22, 2013, 5:43:51 AM7/22/13
to mongod...@googlegroups.com
Hi,

In a data processing application I would like to remove several fields fields from a document and replace it with a new field. The $unset operator works fine for a single field. e.g.

> db.t.insert({"_id" : 1, a : 1, b : 2, c : 3, d : 4});
> db.t.insert({"_id" : 3, a : 1, b : 2, c : 3, d : 4});
> db.t.update({_id : 3}, {$unset : {a : 1} });
> db.t.find();
{ "_id" : 3, "b" : 2, "c" : 3, "d" : 4 }

I gave it a try but $unset does not support arrays and you can not set multiple $unset in the update document since the second just overwrites the first in memory, so if I try that, one of the fields get deleted while the other remains in the document.
Therefore
- I have to send an update to the document for each $unset
- or I have to replace the whole document
- etc?

Which of these is recommended? Is there any plan to implement something like an $unsetall?

I could imagine it like this

> db.t.update({_id : 3}, {$unsetall : ['a','b','c'] });

Thank you,
Laszlo

Asya Kamsky

unread,
Jul 24, 2013, 2:19:24 AM7/24/13
to mongodb-user
Why can't you do multiple field unsets in a single update?  It works fine for me:

db.t.update({_id:3}, {$unset:{a:1, b:1, c:1}})

Just like $set and other update operators.



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