db.foo.remove();
var a10001 = Array(90,0,5,134);
var a10002 = Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,107);
var a10003 = Array(202);
db.foo.insert({
a10001 : a10001,
a10002 : a10002,
a10003 : a10003
});
前两个数组a10001 a10002与插入的第三个a10003相同!
db.foo.find( {} , { "a10003" : 1} );
{ "_id" : ObjectId("534e031ad1844c26ea167205"), "a10003" : [ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ] }
但是当我向第三个数组添加一个元素时
var a10003 = Array(202,1);
表现良好。
{ "_id" : ObjectId("534e0402d1844c26ea167206"), "a10003" : [ 202, 1 ] }
版本:
mongod --version
db version v2.4.9
Wed Apr 16 06:02:33.649 git version: 52fe0d21959e32a5bdbecdc62057db386e4e029c
答案 0 :(得分:0)
我认为你的语法不正确,正如你在Create a JavaScript array containing 1...N看到的那样。这意味着Array(202)创建空数组,其大小为202。
var foo = new Array(202);//create a 202 element array
您可以尝试这样
var foo = new Array();
foo.push(202);
答案 1 :(得分:0)
@Devesh是对的。 当您尝试使用单个参数x创建一个数组对象时,该参数是一个整数(如果它不是一个整数,那么它将创建一个包含该元素的数组,您可以尝试使用boolean或string)然后这意味着您正在初始化一个长度不确定的数组。
如果在插入mongoDB集合时使用该数组,则mongoDB将插入空值。查看我在mongo shell中运行的命令。
> var arr = new Array(1);
> arr
[ undefined ]
> arr.length
1
> var arr = new Array(5)
> arr.length
5
> arr
[ undefined, undefined, undefined, undefined, undefined ]
> var arr = new Array(5,3,4)
> arr.length
3
> arr
[ 5, 3, 4 ]
> var arr = new Array(5)
> arr
[ undefined, undefined, undefined, undefined, undefined ]
> db.testing.insert({arr:arr})
WriteResult({ "nInserted" : 1 })
> db.testing.find().pretty();
{
"_id" : ObjectId("534e0f9fbdb7d435d4bc46d5"),
"arr" : [
null,
null,
null,
null,
null
]
}
> var arr = new Array(5,4,3,2,1)
> db.testing.insert({arr:arr})
WriteResult({ "nInserted" : 1 })
> db.testing.find().pretty();
{
"_id" : ObjectId("534e0f9fbdb7d435d4bc46d5"),
"arr" : [
null,
null,
null,
null,
null
]
}
{
"_id" : ObjectId("534e10c8bdb7d435d4bc46d6"),
"arr" : [
5,
4,
3,
2,
1
]
}
我希望它有所帮助。
您可以参考的其他链接 http://docs.mongodb.org/manual/reference/operator/update-array/ http://docs.mongodb.org/manual/reference/method/db.collection.insert/