MongoDB-Shell脚本-$in参数不起作用
我正在使用MongoDB-Shell脚本-$in参数不起作用,mongodb,robo3t,Mongodb,Robo3t,我正在使用db.system.js.save在服务器上保存js函数 到目前为止我写了这个剧本 function () { cursor = db.MyCollection.find({_id: {$in:[arguments]}}) ^ this does not work while(cursor.hasNext()){print(cursor.next())}; } 我用
db.system.js.save
在服务器上保存js函数
到目前为止我写了这个剧本
function () {
cursor = db.MyCollection.find({_id: {$in:[arguments]}})
^ this does not work
while(cursor.hasNext()){print(cursor.next())};
}
我用以下参数调用它findAllDocuments(ObjectId(“544a30c80c80d5809180b”)代码>,但它不工作
我是否必须以不同的方式调用它参数
?例如,因为参数[0]
正在工作
编辑:为了澄清,参数是javascript函数的参数
findAllDocuments(ObjectId(“544a30c80c80d5809180b”)、ObjectId(“544a30c80c80d5809180b”)代码>
如果我这样使用它,它确实可以工作,但是参数长度可以是可变的
function () {
cursor = db.MyCollection.find({_id: {$in:[arguments[0],arguments[1]}})
^ this does work
while(cursor.hasNext()){print(cursor.next())};
}
编辑2:
如何在$ref中正确使用$in
cursor = db.MyCollection.find({"trainer" : {
"$ref" : "Contact",
"$id" : {$in : [ObjectId("556d901118bfd4901e2a3833")]}
^it executes successfully, but does not find anything
}})
如果arguments
已经是一个数组,则不需要将其传递到另一个数组中:
var arguments = [4, 8, 15, 16, 23, 42];
db.col.find({_id: {$in: arguments}})
如果正在处理的对象不是数组,则必须将其包装到数组中:
db.col.find({_id: {$in: [arguments[0], arguments[1], arguments[2]]}})
请记住,您的$in
运算符将无法处理嵌套数组,因此,如果数组中的每个元素本身就是一个数组,则可能需要将它们合并到一个更大的数组中。我删除了括号并使用了此{u id:{$in:arguments}}
,但我收到了错误“$err”:无法规范化查询:BadValue$需要一个数组“,@MuratK.-由此我们可以推断,参数
实际上不是数组。你能把这个变量的值加到你的帖子里吗?我更新了它。参数是javascript函数的参数。是的,显然参数对象不是真正的数组。它现在正在工作。Thanks@MuratK. - 我现在明白你的意思了arguments
是唯一的变量,而不仅仅是一个名为arguments:)因此,是的,您是正确的。它的外观和行为类似于一个数组,但实际上它不是一个“真实”数组<代码>“类似数组的对象”