Search code examples
node.jsmongodbmonk

How to find by string id using MongoDB, Node and Monk?


I have a document on my collection that has the id "info". I can search on Mongo Atlas easily with {_id: "info"} filter, but when I try with Monk on Node, it tries to create the ObjectID and throw an error. How is the correct way of doing this search on Node?

Search using Atlas console:

enter image description here

const db = monk(url);
const rockets = db.get('rockets');

rockets.find({ _id: "info" }).then((docs) => { 
    console.log(docs);
})

Throw Error:

Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
    at Function.createFromHexString

Solution

  • I just figure it out...

    .find({ '_id': { $eq: "info" } })

    Makes Monk stop trying to cast into ObjectID.