问题
I am trying to update an array object that is nested under another hash. i.e.
{ name: "mike", instagram: { id: 3423, slug: 'mike', photos: [] } }
Now I would like to use $addToSet to append to the instagram photos object but I don't know how. This works to add photos to a new random key but I would like to use the instagram.photos key
MemberCollection.update(this.obj_id, {
$addToSet: {
instagram_photos: {
created_at: new Date(obj.created_time * 1000),
image: obj.images.standard_resolution.url,
type: "instagram_" + obj.type
}
}
});
回答1:
You just need to use the dot notation key like you included in your question:
MemberCollection.update(this.obj_id, {
$addToSet: {
'instagram.photos': {
created_at: new Date(obj.created_time * 1000),
image: obj.images.standard_resolution.url,
type: "instagram_" + obj.type
}
}
});
来源:https://stackoverflow.com/questions/19760004/mongodb-addtoset-of-nested-object