字符串数组的MongoDB大容量插入失败

字符串数组的MongoDB大容量插入失败,mongodb,Mongodb,给定以下数组: arr = ["{'myId': 'myVal1'}","{'myId': 'myVal2'}"] 我可以逐个插入项目,例如 db.collection.insert(arr[0]) 但是当我尝试插入整个数组时,它失败了(如中所述) 我正在使用MongoDB 2.2.4 我如何才能做到这一点?您正在尝试插入一个字符串数组-mongo需要一个json文档数组 “{'foo':'bar'}”是一个字符串。 {'foo':'bar'}是一个对象-一个带有一个键值对的json文档 在

给定以下数组:

arr = ["{'myId': 'myVal1'}","{'myId': 'myVal2'}"]
我可以逐个插入项目,例如

db.collection.insert(arr[0])
但是当我尝试插入整个数组时,它失败了(如中所述)

我正在使用MongoDB 2.2.4


我如何才能做到这一点?

您正在尝试插入一个字符串数组-mongo需要一个json文档数组

“{'foo':'bar'}”
是一个字符串。
{'foo':'bar'}
是一个对象-一个带有一个键值对的json文档

在你看来,当你这样做的时候,插入是成功的

db.collection.insert(“{'foo':'bar'}”)
但它并没有像你想象的那样做

> db.collection.findOne()
{
    "_id" : ObjectId("51941c94c12b0a7bbd416430"),
    "0" : "{",
    "1" : "'",
    "2" : "f",
    "3" : "o",
    "4" : "o",
    "5" : "'",
    "6" : ":",
    "7" : "'",
    "8" : "b",
    "9" : "a",
    "10" : "r",
    "11" : "'",
    "12" : "}",
    "trim" : function __cf__12__f__anonymous_function() {
    return this.replace(/^\s+|\s+$/g, "");
},
    "ltrim" : function __cf__13__f__anonymous_function() {
    return this.replace(/^\s+/, "");
},
    "rtrim" : function __cf__14__f__anonymous_function() {
    return this.replace(/\s+$/, "");
},
    "startsWith" : function __cf__15__f__anonymous_function(str) {
    return this.indexOf(str) == 0;
},
    "endsWith" : function __cf__16__f__anonymous_function(str) {
    return (new RegExp(RegExp.escape(str) + "$")).test(this);
}
}
我已经在你的另一个问题中放了一个指针,询问如何转换这个字符串

> db.collection.findOne()
{
    "_id" : ObjectId("51941c94c12b0a7bbd416430"),
    "0" : "{",
    "1" : "'",
    "2" : "f",
    "3" : "o",
    "4" : "o",
    "5" : "'",
    "6" : ":",
    "7" : "'",
    "8" : "b",
    "9" : "a",
    "10" : "r",
    "11" : "'",
    "12" : "}",
    "trim" : function __cf__12__f__anonymous_function() {
    return this.replace(/^\s+|\s+$/g, "");
},
    "ltrim" : function __cf__13__f__anonymous_function() {
    return this.replace(/^\s+/, "");
},
    "rtrim" : function __cf__14__f__anonymous_function() {
    return this.replace(/\s+$/, "");
},
    "startsWith" : function __cf__15__f__anonymous_function(str) {
    return this.indexOf(str) == 0;
},
    "endsWith" : function __cf__16__f__anonymous_function(str) {
    return (new RegExp(RegExp.escape(str) + "$")).test(this);
}
}