2014-04-05 131 views
4

我試圖刪除嵌入的文件沒有成功。 我在尋找下一條指令Java的方式:MongoDB爪哇拉

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

你想清除嗎? $ unset是刪除文檔的命令。我假設你的意思是刪除一個子文檔。 – hellboy

回答

7

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

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