0
我知道這個問題之前被問及和$ upsert選項回答。哪些現在不再可用?插入的不存在其他更新Java Mongo 3.0
插入很容易,但如何更新文檔如果存在並插入,如果不存在?我嘗試了各種方法無濟於事。
我試過的最後一件事是使用collection.updateOne(..),它帶有兩個Bson類型的參數。那麼,你必須使用toBsonDocument()來轉換一個對象int Bson,但是需要傳遞哪些參數呢? 3.0一個例子將是巨大的......
我暫時的,解決方法是發現和不可insertOne其他updateOne的...
我正在尋找應用$ upsert修飾符,並且不起作用。但是,這確實有效:
db.genres.update({short:"POP"}, {$set: {descr:"Pop Music"}}, {upsert: true})
應翻譯爲:BasicDBObject action = new BasicDBObject("$set", doc); \t \t UpdateOptions options = new UpdateOptions(); \t \t options.upsert(true); \t \t db.genres.updateOne(filter, action, options);
– dexter 2015-04-01 16:59:04