I have an object with the following form :
{
"_id": ObjectId("4fa43f4d1cf26a6a8952adf1"),
"userId": "1",
"facebookId": "1234",
"groups": [{
"groupName": "testGroup",
"members": [{
"memberFirstName": "userFirstName",
"memberLastName": "userLastName",
"memberDetails": {
"userId": "1",
"faceBookId": "1234"
}
}]
}]
}
It's a collection that holds for each user - its groups, with each group containing the group members... So the "groups" key is an array (or list), and so is the "members" key. (each user could have multiple groups, each group has multiple group members).
I'm having a hard time updating the object with a new group member, what I'm trying to do is :
db.collection.update({
userId: "1"
}, {
$push: {
"groups.members": {
membersDetails: {
userId: "2",
faceBookId: "54321"
},
memberFirstName: "UserB",
memberLastName: "UserBLastName"
}
}
});
But it doesn't seem to work.
got the following error :
can't append to array using string field name [members]
I'm also trying to work with the Java driver, but that doesn't seem to work as well.
DBObject query = new BasicDBObject("userId", "1");
DBObject newMemberDetailsObj = new BasicDBObject();
newMemberDetailsObj.put("userId", "2");
newMemberDetailsObj.put("faceBookId", "54321");
DBObject newMemberObj = new BasicDBObject();
newMemberObj.put("memberFirstName", "userB");
newMemberObj.put("memberLastName", "UserBLastName");
newMemberObj.put("memberDetails", newMemberDetailsObj );
DBObject update = new BasicDBObject("$push", new BasicDBObject("members", newMemberObj));
update.put("groupName", "testGroup");
DBObject maintain = new BasicDBObject("$push", new BasicDBObject("groups", update));
WriteResult newWr = coll.update(query, maintain);
This won't work because
groups
is an array, but you're referencing a property in it as if it were an object. You could access things likegroups.0.members
orgroups.1.members
because these refer to specific items in the array, but thegroups
object itself doesn't have amembers
attribute to append to.I'm not totally sure what your update needs to do exactly, but you could add a filter on
groups.groupName
to your query, and then do$push
withgroups.$.members
to append a member only to the group that matched.http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator
It's because your user model can have multiple groups so you need to match a group in the filter and use the positional operator
$
in the update. So your update would be something like this (untested):When you have an array of values, the positional operator basically says "at the position of the matched value in the array".
However, you can only do this one level of arrays deep, so you wouldn't be able to match a specific member in a specific group, for example - so you may want to break up your document into a number of simpler collections if you're going to need to do this sort of thing.