RUEMACHINE
RUEMACHINE

Reputation: 421

Find documents with array of objectIds

I have an array of objectIds represented as strings.

["594c7a30f8f774268a2cdd14","594c7a30f8f774268a2cdd13"]

In my aggregation, I dynamically build a match object with this array along with other fields.

match["_id"] = { $in: classes};

This doesn't work and I assume becuase it's just an array of strings instead of objectIDs like ObjectId("594c7a30f8f774268a2cdd14").

How do I build the match so that _id search works with the array?

Upvotes: 2

Views: 264

Answers (1)

RUEMACHINE
RUEMACHINE

Reputation: 421

I was able to get it to work by changing the match from

match["_id"] = { $in: classes};

to

match._id = { $in: classes.map(mongoose.Types.ObjectId)};

The map function is what I needed.

Upvotes: 2

Related Questions