$unset child array field

1,755 views
Skip to first unread message

MiddleTommy

unread,
Sep 8, 2010, 11:57:39 PM9/8/10
to mongodb-user
$unset does not seem to work on fields in a sub collection of objects
db.employee.update({},{$unset : {"DefaultRates.$.employeeId.$" :
1}},false,true)
db.employee.update({},{$unset : {"DefaultRates.$.employeeId" :
1}},false,true)
db.employee.update({},{$unset : {"DefaultRates.employeeId.$" :
1}},false,true)

All these failed
I wish to delete the subfield employeeId from the DefaultRates sub
collection the field was there to provide WCF Ria Services
compatability but I found a way to not use it in the database.

Scott Hernandez

unread,
Sep 9, 2010, 12:04:15 AM9/9/10
to mongod...@googlegroups.com
"$unset" works fine, but not like that.

To use the positional operator ($) your query must match an array element first. http://mongodb.org/display/DOCS/Advanced+Queries

I'm not really sure what you want; I could hazard a guess but it would be better if you have a sample document as 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.


MiddleTommy

unread,
Sep 9, 2010, 12:28:15 PM9/9/10
to mongodb-user
Here is a sample doc. I need to unset the employeeId field in the
defaultrates array field.
{
_id : 12345,
name : "Tommy",
defaultrates :
[
{ employeeId : 12345, rate : 35.0, raisedOn : "5/2/2009"}
{ employeeId : 12345, rate : 55.0, raisedOn : "5/3/2010"}
{ employeeId : 12345, rate : 65.0, raisedOn : "5/5/2010", comment :
"He is a hard worker"}
]
}


Kristina Chodorow

unread,
Sep 9, 2010, 12:58:15 PM9/9/10
to mongod...@googlegroups.com
You can't do it in a single update.  You could do:

db.employees.update({"_id" : 12345}, {$unset : {"defaultrates.0.employeeId" : 1}});
db.employees.update({"_id" : 12345}, {$unset : {"defaultrates.1.employeeId" : 1}});
db.employees.update({"_id" : 12345}, {$unset : {"defaultrates.2.employeeId" : 1}});

Or fix defaultRates on the client side and use $set to replace the array.




--
Reply all
Reply to author
Forward
0 new messages