MongoDB Java拉

我尝试删除嵌入式文档而没有成功。 我正在寻找以下指令的java方式:

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

Java文档很清楚,你只是构建BSON对象以匹配shell中使用的各自的JSON对应物:

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

使用Bson是类似的。

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