Damian Kelly
Damian Kelly

Reputation: 1

How to build a mongoose query that includes an "or" operator

I am trying to build a mongoose query that will retrieve documents that contain references to any objects whose ids I pass to the query. I am trying to build a query object that includes the $or operator.

My main document contains an attribute which is an array of references to other objects i.e. its schema contains

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

I want to build a query that returns all documents that contain at least one of the tags whose ids I pass into the query - something like.

{"tags":{ $or: ["5759406a4b9b1eb474c25ce4","5759406a4b9b1eb474c25ce3"]} }

which I can pass into the model.find method.

Any help appreciated.

Upvotes: 0

Views: 804

Answers (1)

FlorianE
FlorianE

Reputation: 96

Try the $in operator:

<doc-name>.find({tags: {$in: ["id","id"]} })...

or

<doc-name>.find().where("tags").in(["id","id"])...

Upvotes: 1

Related Questions