-2
A
回答
0
你可能在尋找replaceOne
mongodb的方法。它可以用作:
db.restaurant.replaceOne(
{ "_id" : "1", "somfield" : "Old value" },
{ "_id" : "1", "somfield" : "New value", "additional" : "addedField" }
);
注 - 第一個參數,如果要被替換的文件的過濾器,第二個是將取代它的文檔。
+1
非常感謝你...... –
0
讓我們到外殼,只是來看看的save
方法
> db.test.save
function (obj, opts) {
if (obj == null)
throw Error("can't save a null");
if (typeof(obj) == "number" || typeof(obj) == "string")
throw Error("can't save a number or string");
if (typeof(obj._id) == "undefined") {
obj._id = new ObjectId();
return this.insert(obj, opts);
} else {
return this.update({_id: obj._id}, obj, Object.merge({upsert: true}, opts));
}
}
>
現在,如果我們在以簡單的步驟絕塵而去的邏輯跳躍。
沒有_id
存在的對象
- 創建一個新的ObjectId
- 附加字段對象的集合
- 呼叫
insert
與傳遞選項來保存
_id
存在於對象
- 合併我們與
{upsert: true}
選項傳遞,因此選擇{a: 1, b: 2 }
+{upsert: true}
將與合併後的選項集合{a: 1, b: 2, upsert: true }
- 調用Update,也是對象
_id
相關問題
- 1. sed用替換替換整個行
- 2. 用uuid替換整數ID字段
- 3. 如何替換MongoDB中的整個子文檔數組?
- 4. MongoDB爲什麼id是-1?
- 5. 替換Div ID
- 6. 替換整個字符串
- 7. PHP和mongoDB:一次替換整個集合?
- 8. 使用vimdiff替換整個文件?
- 9. Ajax.BeginForm替換整個頁面
- 10. 替換整個SVN目錄
- 11. Regex.Replace()替換整個事件
- 12. chrome.manifest:替換整個窗口
- 13. FragmentTransaction替換整個屏幕
- 14. 如何替換整個表
- 15. SQL JOIN,用值替換ID
- 16. 在.doc批處理中查找/替換?
- 17. Apache POI Word .DOC替換文本
- 18. 替換字符爲Doc的Docx
- 19. 以編程方式搜索+替換.doc
- 20. 使用ms sql server替換整個單詞「替換」
- 21. 搜索並用替換替換整個字符串
- 22. 用單元格中的整數ID替換字符串 - Matlab
- 23. 使用ANT替換整個XML標記
- 24. 如何用完整對象替換JsonIdentityInfo的id?
- 25. 用NA替換整數(0)
- 26. 如何用sed替換整個行?
- 27. 如何用Greasemonkey替換整個頁面?
- 28. 使用JQuery替換整個表格行
- 29. Javascript替換()與$ 1
- 30. Javascript替換方法,替換爲「$ 1」
的查詢什麼代替? – nullpointer
替換_id爲1的整個文檔。 –
替換時,您將刪除現有文檔並使用較新的文檔進行更新。當時是在問那個文件。 – nullpointer