[Fixed] Mongoose findbyid() return null


I am trying to find a record in my mongo db by its id

No matter I use findbyid(), findone(id,…), it return null

here is my code. what is the solution?

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/Movie', {useNewUrlParser: true})
.then(() => console.log('Connected to Databse'))
.catch(err => console.err('Could not connect to MongoDB', err));

var ObjectId = require('mongodb').ObjectID;

const Schema = new mongoose.Schema({
    name: String,
    author: String,
    tags: [String],
    date: Date, 
    isPublished: Boolean,
    price: Number

const Data = mongoose.model('Datas', Schema);

async function updateData(id){
const result = await Data.findById(id);


I had the same problem. The _id in my DB collection was a String. After I enabled mongoose debug require('mongoose').set('debug', true), I found out that the mongoose query id as ObjectId("yourId") unless we define _id in the Schema. In order to solve the problem I had to add _id:String in to mongoose schema.

const MyDataSchema = new Schema({
  _id: String,

Leave a Reply

(*) Required, Your email will not be published