Mongodb $addToSet of nested object

心已入冬 提交于 2020-01-15 04:40:12

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!