Categories
JavaScript Answers

How to fix can’t find documents searching by ObjectId using Node Mongoose?

Spread the love

To fix can’t find documents searching by ObjectId using Node Mongoose, we convert the object ID string to an object ID.

For instance, we write

const ObjectId = require("mongoose").Types.ObjectId;
const query = { campaign_id: new ObjectId(campaign._id) };

to set query to an object with the campaign+id to an object ID that we create by calling the ObjectId constructor with the campaign._id string.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *