简体   繁体   中英

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:

在此处输入图像描述

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

I just figure it out...

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

Makes Monk stop trying to cast into ObjectID.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM