我有一个json对象如下:
{ "_id" : ObjectId("508806803bb97dc546e6f307"), "user_name" : "user1", "user_id" : 45645645, "likes" : [ { "event_id" : NumberLong("4578541212") },{ "event_id" : NumberLong("4578541213") } ], "dislikes" : [ ] }
我正在尝试通过java驱动程序删除likes数组中的特定事件 尝试在shell中首先执行此操作:
> db.users.update( {'likes.event_id' : 4578541212}, { '$unset':{'likes.event_id'
:1}})
没有运气......我怎么能做到这一点?
答案 0 :(得分:1)
如果您只想从数组元素中删除event_id
字段:
db.users.update( {'likes.event_id' : 4578541212}, {'$unset':{'likes.$.event_id' :1}})
使用$pull
运算符删除元素:
db.users.update({'likes.event_id': 4578541212}, {'$pull':{likes: {event_id: 4578541212}}})