2015-03-31 49 views
0

我知道這個問題之前被問及和$ upsert選項回答。哪些現在不再可用?插入的不存在其他更新Java Mongo 3.0

插入很容易,但如何更新文檔如果存在並插入,如果不存在?我嘗試了各種方法無濟於事。

我試過的最後一件事是使用collection.updateOne(..),它帶有兩個Bson類型的參數。那麼,你必須使用toBsonDocument()來轉換一個對象int Bson,但是需要傳遞哪些參數呢? 3.0一個例子將是巨大的......

我暫時的,解決方法是發現和不可insertOne其他updateOne的...

+1

我正在尋找應用$ 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

回答

0

這很奇怪,因爲在API文檔3.0的更新插入選項依然存在。 DBCollection#update

編輯: 好吧我猜你正在使用MongoCollection ...你應該使用updateOne方法,UpdateOptions設置爲true。

相關問題