DEV Community

loading...

#9 ObjectId

rittwickbhabak profile image Rittwick Bhabak ・1 min read

To retrieve data by ID from the collection:

MarioChar.findOne({ _id: character._id }).then(result => {
    assert(result._id === character._id)
    done();
})
Enter fullscreen mode Exit fullscreen mode

But the above code is not going to work as
result._id===character._id is going to return false as
result._id is of type ObjectID and character._id is of type String. So we've to convert both of them to String and then compare.
result._id.toString()===character._id.toString()
will return true

Discussion (0)

pic
Editor guide