9951 explained code solutions for 126 technologies


mongodbHow to use findone in MongoDB?


MongoDB's findOne() method is used to query a collection and return a single document that matches the specified query criteria.

Example

db.collection.findOne({name: "John"})

Output example

{
  "_id": ObjectId("5f3d7f8d3f9f8d7f8d7f8d7f"),
  "name": "John",
  "age": 25
}

Code explanation

  • db.collection: specifies the collection to query
  • findOne(): the method used to query the collection
  • {name: "John"}: the query criteria, in this case, the document must have a name field with the value John

Helpful links

Edit this code on GitHub