These are chat archives for Automattic/mongoose

5th
Jun 2018
Chris Rutherford
@cjrutherford
Jun 05 2018 18:59 UTC

So.... I'm trying to get a list of ID's from the database, and I'm trying to do it like this:

router.get('/list', (req,res) => {
Model.findAll({}, {_id}).then(...)
});

and I'm getting this error:

Cast to ObjectId failed for value "list" at path "_id" for model "insiders"
CastError: Cast to ObjectId failed for value "list" at path "_id" for model "insiders"
at new CastError (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\error\cast.js:27:11)
at ObjectId.cast (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\schema\objectid.js:158:13)
at ObjectId.SchemaType.applySetters (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\schematype.js:724:12)
at ObjectId.SchemaType._castForQuery (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\schematype.js:1113:15)
at ObjectId.SchemaType.castForQuery (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\schematype.js:1103:15)
at ObjectId.SchemaType.castForQueryWrapper (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\schematype.js:1082:15)
at cast (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\cast.js:300:32)
at model.Query.Query.cast (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\query.js:3309:12)
at model.Query.Query._castConditions (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\query.js:1293:10)
at model.Query.Query._findOne (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\query.js:1518:8)
at process.nextTick (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\kareem\index.js:333:33)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)

I'm not sure what this error means, but I'm sure I'm not filtering out the other properties properly.... I think....

Kev
@lineus
Jun 05 2018 19:01 UTC
@cjrutherford what is Model.findAll() ?
Chris Rutherford
@cjrutherford
Jun 05 2018 19:01 UTC
I think I was mis-remembering things again @lineus
I was thinking that I needed to use findAll() instead of just find() to get "All the things"
Basically I just want to get only the ids for all the model objects in the database.
Just like last time, 'it's not an array, it's not an object, just a space separated string.' Am I the only one confused by that?
Kev
@lineus
Jun 05 2018 19:05 UTC
no, you're right it's an object
I was wrong
Chris Rutherford
@cjrutherford
Jun 05 2018 19:05 UTC
OH!
Kev
@lineus
Jun 05 2018 19:06 UTC
it does get hard to remember :)
so it would be Model.find({}, { _id: 1 }) I think hehe
Chris Rutherford
@cjrutherford
Jun 05 2018 19:08 UTC
hmmmm, was going to say that it still didn't work, but let's see how that works
Hrmm..... well I mean I just want the ID's of the objects, and that's it. How would you go about it @lineus ?
still not working as expected.....
Kev
@lineus
Jun 05 2018 19:11 UTC
but then again, my example is hella simple
Chris Rutherford
@cjrutherford
Jun 05 2018 19:16 UTC
Thought I found it with Populate()but I was still getting the same error.... Think it's time to give out the full Express route though.
router.get('/list', (req, res) => {
Insider.find({}, '_id').then(insiders => {
if (!insiders) {
res.status(400).json({ error: 'unable to find list of insiders' });
}
res.json(insiders);
}).catch(err => res.status(400).json(err));
});
Kev
@lineus
Jun 05 2018 19:17 UTC
'_id' should be { _id: 1 }
Chris Rutherford
@cjrutherford
Jun 05 2018 19:17 UTC
yes, I've tried it with that, but I still get this "casting" error.....
 Cast to ObjectId failed for value "list" at path "_id" for model "insiders"
CastError: Cast to ObjectId failed for value "list" at path "_id" for model "insiders"
at new CastError (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\error\cast.js:27:11)
at ObjectId.cast (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\schema\objectid.js:158:13)
at ObjectId.SchemaType.applySetters (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\schematype.js:724:12)
at ObjectId.SchemaType._castForQuery (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\schematype.js:1113:15)
at ObjectId.SchemaType.castForQuery (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\schematype.js:1103:15)
at ObjectId.SchemaType.castForQueryWrapper (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\schematype.js:1082:15)
at cast (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\cast.js:300:32)
at model.Query.Query.cast (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\query.js:3309:12)
at model.Query.Query._castConditions (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\query.js:1293:10)
at model.Query.Query._findOne (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\query.js:1518:8)
at process.nextTick (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\kareem\index.js:333:33)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
Kev
@lineus
Jun 05 2018 19:19 UTC
are your _id's ObjectIds? or are they something else?
Chris Rutherford
@cjrutherford
Jun 05 2018 19:19 UTC
I don't even see how '/list' is getting into the error... and they should be ObjectIds. I haven't explicitly set an ID field myself.
Kev
@lineus
Jun 05 2018 19:23 UTC
what does mongodb shell show as the _id field in those docs?
do you see any that look off?
Chris Rutherford
@cjrutherford
Jun 05 2018 19:24 UTC
noop, looks normal here.
Kev
@lineus
Jun 05 2018 19:26 UTC
yeah, failed for value "list" is really troubling.
so the cast error is what you get when you use the object projection? like { _id: 1}?
Chris Rutherford
@cjrutherford
Jun 05 2018 19:29 UTC
it's really odd, '/list' is just my route, that shouldn't be anywhere in my database. It's been with every iteration we've tried today.
Kev
@lineus
Jun 05 2018 19:30 UTC

let's see what using the native driver directly does.

replace

Insider.find({}, { _id: 1}).then(etc)

with

Insider.collection.find({},{ _id: 1}).then(etc)
Chris Rutherford
@cjrutherford
Jun 05 2018 19:31 UTC
okay, even odder. I just changed the route from /list to /jerry and the error changed from list.... to jerry.... Very odd....
still the same.... I'm really not sure what's going on here....
going to stack overflow this one.
Kev
@lineus
Jun 05 2018 19:35 UTC
what versions of express and mongoose are you using @cjrutherford ?
Chris Rutherford
@cjrutherford
Jun 05 2018 19:36 UTC
node 8.11.2, express 4.16.3, mongoose 5.1.3
Kev
@lineus
Jun 05 2018 19:36 UTC
can you humor me and do a search in the mongo shell db.insiders.find({ _id: 'list' }) ?
I'll set up a test instance with those packages and see if I can duplicate it.
Chris Rutherford
@cjrutherford
Jun 05 2018 19:40 UTC
No Records like that. Confirmed.
Kev
@lineus
Jun 05 2018 19:40 UTC
damn, I knew it wouldn't be that easy :worried:
Kev
@lineus
Jun 05 2018 19:42 UTC
I think you should have return on this lineres.status(400).json({ error: 'unable to find list of insiders' });
Alexander Balyshyn
@limpid-kzonix
Jun 05 2018 19:42 UTC

Hi, and help me) for example I have document

{
"index": 2,
"retryAfter": [100, 1000, 10000, 1000000]
}

how to build query to retrieve such element from inner array as value of index field?

Chris Rutherford
@cjrutherford
Jun 05 2018 19:43 UTC
@lineus let me try that!
no dice....
it's not a big requirement right now, so I'll hold off for the SO question. Don't rack your brain for it @lineus
Kev
@lineus
Jun 05 2018 19:45 UTC
that's how I learn man. don't take that away from me!
@limpid-kzonix let me take a crack at it
Chris Rutherford
@cjrutherford
Jun 05 2018 19:46 UTC
lol, it's all good, I just don't like being too much of a burden on people
Kev
@lineus
Jun 05 2018 19:47 UTC
no burden on me :) it's fun!
Kev
@lineus
Jun 05 2018 20:01 UTC
@limpid-kzonix gist here
I hope I understood your question correctly :smiley:
Kev
@lineus
Jun 05 2018 21:44 UTC
@cjrutherford you SO question has Insider.find({}, {_id}) it needs to be Insider.find({}, {_id: 1 })
Kev
@lineus
Jun 05 2018 22:02 UTC
@cjrutherford after looking at the source code, you CAN use a string there. The API docs say it's an object in Model.find() but Model.find() just passes the arg off to query's select method. I just tested this out, '+_id' , on an express route and it works just like the above object.