xxxxxxxxxx
To find one document in MongoDB using the findOne method,
you can use the following code in Node.js:
const result = await yourCollection.findOne({ yourQuery });
Here 'yourCollection' is the actual name of your MongoDB collection and
'yourQuery' is the criteria you want to search for in the document.
xxxxxxxxxx
itemsCollection.findOne(query, projection)
.then(result => {
if(result) {
console.log(`Successfully found document: ${result}.`);
} else {
console.log("No document matches the provided query.");
}
return result;
})
.catch(err => console.error(`Failed to find document: ${err}`));
xxxxxxxxxx
// mongo shell findOne example
db.students.findOne({"_id": "6139d13394f6b376e447ff9d"})