.find() by array of objectId

11,613 views
Skip to first unread message

Ryan Kelly

unread,
Apr 12, 2013, 8:46:59 AM4/12/13
to mongoo...@googlegroups.com
hello, I am trying to verify that a list of documents that are passed in actually exist. However, when I pass the array of ids in, I get an Object Cast error. 

For example,
works ---> Market.find({ _id: { $in: ["516741e4b8ac79000000000a","516741f0b8ac79000000000b"]}}, function (err, markets) {
doesn't work ----> Market.find({ _id: { $in: [v.markets]}}, function (err, markets) {

The error I get looks like this:
"Cast to ObjectId failed for value \"516741f0b8ac79000000000a,51674221b8ac79000000000b\" at path \"_id\""

Any ideas as to why this might be failing?

Aaron Heckmann

unread,
Apr 12, 2013, 5:04:21 PM4/12/13
to mongoo...@googlegroups.com
Is `v.markets` an array? If so, do not re-wrap it in another array.


--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Aaron


Ryan Kelly

unread,
Apr 12, 2013, 10:24:48 PM4/12/13
to mongoo...@googlegroups.com
Thanks Aaron, yes v.markets is an array. I also have a findById method where I tried what you suggested and I receive a mongoError that says it is an invalid query. 

Here is what the log looks like - venues.find({ markets: { '$in': '516741f0b8ac79000000000b,516741f0b8ac79000000000b' } })
Here is what my line of code looks like - Venue.find({ markets: { $in: markets}} , function(err,venues) {

Aaron Heckmann

unread,
Apr 13, 2013, 2:23:58 AM4/13/13
to mongoo...@googlegroups.com
$in accepts an array. Are you passing something else?

Ryan Kelly

unread,
Apr 13, 2013, 10:24:32 AM4/13/13
to mongoo...@googlegroups.com
That was it! I was passing them on the querystring - /venues?markets=516741f0b8ac79000000000b,516741f0b8ac79000000000b. So I split() the string and created an array. Everything is working now. thank you!
Message has been deleted

Ryan Wheale

unread,
Jul 9, 2015, 11:41:37 AM7/9/15
to mongoo...@googlegroups.com
Sharry, the ID you are passing in is actually two ids joined by a comma, which looks like the result of an array getting joined. Look at the error:

CastError: Cast to ObjectId failed for value "5594f660285cf1121673cfd2,559e7061285cf1121673cfe8" at path "_id"

Internally, mongoose needs to convert your String IDs to ObjectIDs. This is done by passing the string to the ObjectID constructor:

var objID = new ObjectID("5594f660285cf1121673cfd2");

Your code is throwing an error because it thinks the ID is the long string: "5594f660285cf1121673cfd2,559e7061285cf1121673cfe8"

You need to figure out why your array is getting join()'d somewhere.

On Thu, Jul 9, 2015 at 7:30 AM, Sharry Stowell <sha...@molinto.com> wrote:
Same for me, I get an error:
CastError: Cast to ObjectId failed for value "5594f660285cf1121673cfd2,559e7061285cf1121673cfe8" at path "_id"

using:
var items = [ '5594f660285cf1121673cfd2', '559e7061285cf1121673cfe8' ];
Product.find({ "_id": { $in: [ items ] } }, { type: 1}).exec(....

Thanks
Sharry
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages