1

I am using db.system.js.save to save js functions on the server.

I wrote this script so far

function () {
cursor = db.MyCollection.find({_id: {$in:[arguments]}})
                                             ^ this does not work
        while(cursor.hasNext()){print(cursor.next())};       
}

I am calling it with the following argument findAllDocuments(ObjectId("544a30c80c80d5809180b"),<more objects>);, but it is not working.

Do I have to call it arguments differently? Because arguments[0] is working for example.


Edit: For clarification arguments are the parameters of the javascript function.

findAllDocuments(ObjectId("544a30c80c80d5809180b"),ObjectId("544a30c80c80d5809180b"), <and more objectIds>); If I use it like this it does work, but the arguments length can be variable.

function () {
cursor = db.MyCollection.find({_id: {$in:[arguments[0],arguments[1]}})
                                             ^ this does work
        while(cursor.hasNext()){print(cursor.next())};       
}

EDIT 2:

How can I use the $in correctly in $ref?

 cursor = db.MyCollection.find({"trainer" : {
        "$ref" : "Contact",
        "$id" : {$in : [ObjectId("556d901118bfd4901e2a3833")]}
                  ^it executes successfully, but does not find anything
    }})
Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107

1 Answers1

2

If arguments is already an array you don't need to pass it inside another array:

var arguments = [4, 8, 15, 16, 23, 42];
db.col.find({_id: {$in: arguments}})

If the objects you are working with are not arrays you have to wrap them within an array:

db.col.find({_id: {$in: [arguments[0], arguments[1], arguments[2]]}})

Keep in mind that your $in operator won't be able to work with nested arrays so if each element of your array is an array itself you might need to merge them together into one larger array.

Lix
  • 47,311
  • 12
  • 103
  • 131
  • I removed the brackets and went with this `{_id: {$in:arguments}}`, but I am getting the error _"$err" : "Can't canonicalize query: BadValue $in needs an array",_ – Murat Karagöz Jun 02 '15 at 13:01
  • @MuratK. - so from this we can deduce that `arguments` is not in fact an array. Can you add to your post the value of that variable? – Lix Jun 02 '15 at 13:03
  • I updated it. The arguments are the parameters of the javascript function. – Murat Karagöz Jun 02 '15 at 13:07
  • @MuratK. - so you are saying that the `arguments` variable has this sort of value: `[ ObjectId(...), ObjectId(...), ObjectId(...), ObjectId(...), ...]`? Is that the variable that you are working with? – Lix Jun 02 '15 at 13:10
  • 1
    Yes it was, apparently arguments Object is not a _real_ array. [I had to use this to make it a array var args = Array.prototype.slice.call(arguments);](http://stackoverflow.com/questions/960866/how-can-i-convert-the-arguments-object-to-an-array-in-javascript). It is now working. Thanks – Murat Karagöz Jun 02 '15 at 13:11
  • 1
    @MuratK. - ohhhhh I see what you mean now. `arguments` was the [actual `arguments` object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments) and not just a variable called arguments :) So yes, you are correct. It looks and behaves like an array but it's not actually a "real" array. [The docs call it](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments) `"an Array-like object"` – Lix Jun 02 '15 at 13:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79424/discussion-between-murat-k-and-lix). – Murat Karagöz Jun 02 '15 at 13:36
  • One quick question, could you look at my Edit2 pls? – Murat Karagöz Jun 02 '15 at 13:37
  • @MuratK. - you might want to open a new post for a separate question. Sorry, but I can't chat ATM. – Lix Jun 02 '15 at 13:43