2012-05-31 72 views
-1

或...「對我來說很複雜」可能更準確。複雜的蒙戈插入

在任何情況下,我的mongod和蒙戈開放和蒙戈終端我把下面的命令:

db.err_details.insert({"error_text" : "Log has found error on inbound", "co 
de" : "WM2001_0", "product" : "WM2001", "profile : ["CR" : "11999002", "sol 
s" : ["run", "to", "the", "hills"]], "otherinfo" : "Contact Bob Saget"}); 

err_details確實存在,不是的事項。無論如何,我按下回車鍵,它會給我兩次「...」,然後退出而不插入。它在查詢語法錯誤之前就做到了這一點......但它並沒有告訴你錯誤在哪裏或錯在哪裏,而且,這次我找不到它。

如果意圖不清楚,我希望「配置文件」具有一組鍵值對,其中一個(「sols」)具有ITS OWN數組值,而不是鍵值對。

在實際上「輪廓」的光爲對象我嘗試以下:

db.err_details.insert({"error_text" : "Log has found error on inbound", "errco 
de" : "WM2001_0", "product" : "WM2001", "profile" : {"CR" : "11999002", "sols" : 
["run", "to", "the", "hills"]}, "otherinfo" : "Contact Bob Saget"}); 

這是有效的蒙戈但我想對象的數組,而不是包含與數組中的元素中的對象。

+0

你最後的例子看起來不錯。什麼不工作? –

+0

最後一個例子是一個很好的......減去一個缺失的「在CR之後,這就是說,這並不完全是我在尋找的概念, – PinkElephantsOnParade

回答

4

存在語法錯誤。你的「profile」屬性的值似乎是一個對象,但是你使用數組的[]。還有一個引用周圍的配置文件也是缺失的。 (提示:只需將您的命令複製/粘貼到任何JS檢查服務,因爲這是Mongo控制檯所使用的,帶有即時錯誤突出顯示的編輯將非常出色)。

我不太明白你的數據庫的結構,但「K/V對數組」應該是類似的規定:

db.err_details.insert({ 
    "error_text" : "Log has found error on inbound", 
    "code" : "WM2001_0", 
    "product" : "WM2001", 
    "profile" : [ 
     {"CR" : "11999002"}, 
     {"sols" : ["run", "to", "the", "hills"]} 
    ], 
    "otherinfo" : "Contact Bob Saget" 
}); 

即 - 與每對中的物體包裹起來。

+0

實際上,我認爲你會發現''profile'應該是'」配置文件「'可能以及...... – pms1969

+0

良好的捕獲。也添加了它。 –

+0

使用它 - 正是我想要的。再次感謝! – PinkElephantsOnParade

1

數組,[]在BSON/JSON/JavaScript中不能被鍵入,您需要使用一個對象{}。您的Profile屬性具有鍵,但鍵入爲數組。

+0

Nah,「sols」是好的。只有「個人資料」是不好的。 –

+0

oops MB誤讀了它。 – JaredMcAteer