I am in a situation that feels a little odd ... I have a list of keys deliberately containing duplicates. For the sake of the arguments lets assume this list looks like [1,2,3,2,1]
. Currently the code to fetch the documents belonging to these Ids loops over the list of keys, calls findOne()
and pushes the document into an array.
So we have a construct like this:
for (var i = 0; i < keys.length; i++) {
documents.push(db.items.findOne(keys[i]);
}
I am wondering whether there is a way to do this in a more ... elegant ... fashion, preferably with a single query? Keeping the order would be a plus but is not strictly required.
Edit: Please note that this is a MongoDB question. I am looking for a way to substitute the above loop with a single call to db.items.find().