DEV Community

Full Stack Hacker
Full Stack Hacker

Posted on • Edited on

1 1

Getting data in MongoDB (part 1)

The findOne() returns a single document from a collection that satisfies the specified condition.

The findOne() method has the following syntax:

db.collection.findOne(query, projection)

The findOne() accepts two optional arguments: query and projection.

  • The query is a document that specifies the selection criteria.
  • The projection is a document that specifies the fields in the matching document that you want to return.

If you omit the query, the findOne() returns the first document in the collection according to the natural order which is the order of documents on the disk.

If you don’t pass the projection argument, then findOne() will include all fields in the matching documents.

By default, MongoDB always includes the _id field in the returned documents. To suppress it, you need to explicitly specify _id: 0 in the projection argument.

If multiple documents satisfy the query, the findOne() method returns the first document based on the order of documents stored on the data storage.

MongoDB findOne() method examples

First, launch mongo shell from the Terminal on Linux and connect to the productdb database on the local MongoDB server: mongosh productdb

Using MongoDB insertMany() method to insert multiple documents:

db.products.insertMany([
    { "_id" : 1, "name" : "xPhone", "price" : 799, "releaseDate": ISODate("2011-05-14"), "spec" : { "ram" : 4, "screen" : 6.5, "cpu" : 2.66 },"color":["white","black"],"storage":[64,128,256]},
    { "_id" : 2, "name" : "xTablet", "price" : 899, "releaseDate": ISODate("2011-09-01") , "spec" : { "ram" : 16, "screen" : 9.5, "cpu" : 3.66 },"color":["white","black","purple"],"storage":[128,256,512]},
    { "_id" : 3, "name" : "SmartTablet", "price" : 899, "releaseDate": ISODate("2015-01-14"), "spec" : { "ram" : 12, "screen" : 9.7, "cpu" : 3.66 },"color":["blue"],"storage":[16,64,128]},
    { "_id" : 4, "name" : "SmartPad", "price" : 699, "releaseDate": ISODate("2020-05-14"),"spec" : { "ram" : 8, "screen" : 9.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256,1024]},
    { "_id" : 5, "name" : "SmartPhone", "price" : 599,"releaseDate": ISODate("2022-09-14"), "spec" : { "ram" : 4, "screen" : 5.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256]}
 ])

The following example uses the findOne() method to select the first document from the products collection: db.products.findOne() or db.products.findOne({})

The following statement uses the findOne() method to find the document whose _id is 2:

db.products.findOne({_id:2})

The following example uses the findOne() method to find the document with _id 5. And it returns only the _id and name fields of the matching document:

db.products.findOne({_id: 5}, {name: 1})

As you can see clearly from the output, MongoDB includes the _id field in the returned document by default. To completely remove it from the returned document, you need to explicitly specify _id:0 in the projection document like this:

db.products.findOne({ _id: 5 }, {name: 1, _id: 0})

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay