These are chat archives for Automattic/mongoose

Jun 2016
Damian Kelly
Jun 09 2016 08:58


I have a main document which includes an attribute which is an array of references to other objects


tags: [{type:mongoose.Schema.Types.ObjectId, ref:'Tag'}],

I would like to be able to programmatically query my collection of main documents to find
documents that contain specific "tags".

This seems to work ok if I pass a single "tag" _id string as follows (as parameter to model.find)


Mongoose automatically casts the id string to an ObjectId and returns the correct result.

If however I want to pass more than one "tag" value I get an error

"Cast to ObjectId failed for value \"575922ead05badf46ba49267,575922ead05badf46ba49266\" at path \"tags\"","name":"CastError","kind":"ObjectId","value":"575922ead05badf46ba49267,575922ead05badf46ba49266","path":"tags"}

I have tried using




Any help to point me in the right direction for the correct syntax / documentation would be appreciated.

Mohamed Nasrullah
Jun 09 2016 09:38
use async.eachSeries to loop through tags @z424brave
Damian Kelly
Jun 09 2016 11:42

Hi Mohamed

Thanks for the response.

Are you saying I need to omit the "tags" criteria from the model.find and then iterate through
all the main documents matching the tags in my own callback to filter just the documents I want.

Mohamed Nasrullah
Jun 09 2016 12:14
Damian Kelly
Jun 09 2016 12:50

Actually it was a mistake on my part - building a query object like below does work and is giving me what I need

{"tags":{$in: ["575964e05f50c5dc77d47593","575964e05f50c5dc77d47592"]}}

this is returning any document with either of the supplied tags

thanks for the help anyway