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