MongoDB Java pull

坚强是说给别人听的谎言 提交于 2019-12-19 19:43:48

问题


i tried to remove an embedded document without succcess. I'm looking for the java way of following instruction:

db.games.update({'_id': 73}, {$pull: {'goals': {'goal': 4}}})

回答1:


The Java documentation is pretty clear, you are just constructing BSON objects to match their respective JSON counterparts as used in the shell:

    BasicDBObject query = new BasicDBObject("_id", 73);
    BasicDBObject fields = new BasicDBObject("goals", 
        new BasicDBObject( "goal", 4));
    BasicDBObject update = new BasicDBObject("$pull",fields);

    games.update( query, update );



回答2:


Using Bson is similar.

Bson query = new Document().append("_id", 73);
Bson fields = new Document().append("goals", new Document().append( "goal", 4));
Bson update = new Document("$pull",fields);

games.updateOne( query, update );


来源:https://stackoverflow.com/questions/22875150/mongodb-java-pull

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