Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
console.log(result);
}
updateData('5a6900fff467be65019a9001');
like image 399
Kenny Avatar asked Sep 03 '18 10:09

Kenny


People also ask

Does findById return null?

Return value findById returns the document where the _id field matches the specified id . If the document is not found, the function returns null .

What does findById return if not found?

std::find. Returns an iterator to the first element in the range [first,last) that compares equal to val . If no such element is found, the function returns last .

What is findById in mongoose?

Mongoose | findById() Function The findById() function is used to find a single document by its _id field. The _id field is cast based on the Schema before sending the command. Installation of mongoose module: You can visit the link Install mongoose module. You can install this package by using this command.

What does findById return in Nodejs?

findById() function is used to find one document by its _id . The findById() function takes in a single parameter, the document id. It returns a promise that resolves to the Mongoose document if MongoDB found a document with the given id , or null if no document was found.


Video Answer


5 Answers

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,
...
...
}
like image 108
Pramod Avatar answered Nov 06 '22 16:11

Pramod


Check your mongodb database, if _id is storaged as String, findById(id) can not found id. FindById(id) only finds ObjectId('yourId'). You might import database by using mongoimport and including _id in JSON, it's wrong, delete _id in imported JSON.

like image 40
Khánh Thọ Nguyễn Avatar answered Nov 06 '22 17:11

Khánh Thọ Nguyễn


I was also facing the same issue. Then I referred to this solution Make sure the documents in the collection have _id as an Object rather than a String. If you have imported the database entries using a JSON file, just remove all the _id fields and then import it.

After importing, MongoDB will itself assign the _id values to all the entries.

like image 33
saurabhlahoti Avatar answered Nov 06 '22 17:11

saurabhlahoti


There is a simple solution to this:

Replace

const result = await Data.findById(id);

with

const result = await Data.findById(id).exec();

See Mongoose - What does the exec function do? for an explanation on what exec() does

like image 43
Sylvestaro Avatar answered Nov 06 '22 18:11

Sylvestaro


After hour of searching i needed to add _id to Schema like this:

  _id: { type: Schema.Types.ObjectId },
like image 35
mradex77 Avatar answered Nov 06 '22 17:11

mradex77