复杂的Mongo插入

时间:2012-05-31 14:39:20

标签: mongodb

或者......“对我来说很复杂”可能更准确。

无论如何,我打开了mongod和mongo,在mongo终端我输入了以下命令:

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"});

错误_details存在,而不是重要。在任何情况下,我按回车键,它给我“......”两次,然后退出而不插入。它已经在查询中出现语法错误之前完成了这个...但它并没有告诉你错误在哪里或出错了什么,而且,这次我找不到它。

如果目的不明确,我希望“profile”有一组键值对,其中一个(“sols”)ITS OWN数组只是值,而不是键值对。

鉴于“profile”是一个对象,我尝试了以下内容:

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"});

这是有效的mongo,但我想要一个对象数组,而不是一个包含带数组的元素的对象。

2 个答案:

答案 0 :(得分: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"
});

即。 - 每对都包裹在物体中。

答案 1 :(得分:1)

无法键入BSON / JSON / JavaScript中的数组[],您需要使用对象{}。您的Profile属性具有键,但键入为数组。