w3resource

MongoDB: db.collection.findOne() method

db.collection.findOne

The db.collection.findOne() method is used to return a single document that satisfy the specified query criteria. If multiple documents satisfy the query, this method returns the first document according to the natural order which reflects the order of documents on the disk. In capped collections, the natural order is the same as insertion order. If no document satisfies the query, the method returns null.

Syntax:

db.collection.findOne(query, projection)

Parameter:s

Name Description Required /
Optional
Type
query Specifies query selection criteria using query operators Optional document
projection Specifies the fields to return using projection operators. Omit this parameter to return all fields in the matching document. Optional document

The projection parameter takes a document of the following form:

{ field1: <boolean>, field2: <boolean> ... }

The <boolean> can be one of the following includes or exclude values:

  • 1 or true to include. The findOne() method always includes the _id field even if the field is not explicitly specified in the projection parameter.
  • 0 or false to exclude.

The projection argument cannot mix include and exclude specifications, with the exception of excluding the _id field.

Returns

One document that satisfies the criteria specified as the first argument to this method. If you specify a projection parameter, findOne() returns a document that only contains the projection fields. The _id field is always included unless you explicitly exclude it.

Sample document in the restaurants collection:


{
  "address": {
     "building": "1007",
     "coord": [ -73.856077, 40.848447 ],
     "street": "Morris Park Ave",
     "zipcode": "10462"
  },
  "borough": "Bronx",
  "cuisine": "Bakery",
  "grades": [
     { "date": { "$date": 1393804800000 }, "grade": "A", "score": 2 },
     { "date": { "$date": 1378857600000 }, "grade": "A", "score": 6 },
     { "date": { "$date": 1358985600000 }, "grade": "A", "score": 10 },
     { "date": { "$date": 1322006400000 }, "grade": "A", "score": 9 },
     { "date": { "$date": 1299715200000 }, "grade": "B", "score": 14 }
  ],
  "name": "Morris Park Bake Shop",
  "restaurant_id": "30075445"
}
.....

Example: MongoDB: db.collection.findOne() method with Empty Query Specification

The following operation returns a single document from therestaurants collection.

db.restaurants.findOne();

Output:

{
        "_id" : ObjectId("55c3043ab165fa6355ec5c89"),
        "address" : {
                "building" : "1007",
                "coord" : [
                        -73.856077,
                        40.848447
                ],
                "street" : "Morris Park Ave",
                "zipcode" : "10462"
        },
        "borough" : "Bronx",
        "cuisine" : "Bakery",
        "grades" : [
                {
                        "date" : ISODate("2014-03-03T00:00:00Z"),
                        "grade" : "A",
                        "score" : 2
                },
                {
                        "date" : ISODate("2013-09-11T00:00:00Z"),
                        "grade" : "A",
                        "score" : 6
                },
                {
                        "date" : ISODate("2013-01-24T00:00:00Z"),
                        "grade" : "A",
                        "score" : 10
                },
                {
                        "date" : ISODate("2011-11-23T00:00:00Z"),
                        "grade" : "A",
                        "score" : 9
                },
                {
                        "date" : ISODate("2011-03-10T00:00:00Z"),
                        "grade" : "B",
                        "score" : 14
                }
        ],
        "name" : "Morris Park Bake Shop",
        "restaurant_id" : "30075445"
}

Example: MongoDB: db.collection.findOne() method with a Query Specification

The following operation returns the first matching document from therestaurants collectionwhere either the field namein the documentstarts with the letterGorwhere the fieldcoord in the embedded document address is more than90.

db.restaurants.findOne(
   {
     $or: [
            { "name" : /^G/ },
            { "address.coord": { $gt: 90 } }
          ]
   }
);

Output

{
        "_id" : ObjectId("55c3043ab165fa6355ec5c9b"),
        "address" : {
                "building" : "522",
                "coord" : [
                        -73.95171,
                        40.767461
                ],
                "street" : "East   74 Street",
                "zipcode" : "10021"
        },
        "borough" : "Manhattan",
        "cuisine" : "American ",
        "grades" : [
                {
                        "date" : ISODate("2014-09-02T00:00:00Z"),
                        "grade" : "A",
                        "score" : 12
                },
                {
                        "date" : ISODate("2013-12-19T00:00:00Z"),
                        "grade" : "B",
                        "score" : 16
                },
                {
                        "date" : ISODate("2013-05-28T00:00:00Z"),
                        "grade" : "A",
                        "score" : 9
                },
                {
                        "date" : ISODate("2012-12-07T00:00:00Z"),
                        "grade" : "A",
                        "score" : 13
                },
                {
                        "date" : ISODate("2012-03-29T00:00:00Z"),
                        "grade" : "A",
                        "score" : 11
                }
        ],
        "name" : "Glorious Food",
        "restaurant_id" : "40361521"
}

Example: MongoDB: db.collection.findOne() method excluding the specified Fields

The following operation returns a document in therestaurants collectionwhere thecuisine field contains the elementItalianand returns all fieldsexceptthe_id field and grades field.

db.restaurants.findOne(
   { "cuisine": "Italian" },
   { _id: 0, grades: 0 }
);

Output:

{
        "address" : {
                "building" : "10004",
                "coord" : [
                        -74.03400479999999,
                        40.6127077
                ],
                "street" : "4 Avenue",
                "zipcode" : "11209"
        },
        "borough" : "Brooklyn",
        "cuisine" : "Italian",
        "name" : "Philadelhia Grille Express",
        "restaurant_id" : "40364305"
}

Retrieve the restaurants data from here

Previous: db.collection. findAndModify() method
Next: db.collection.getIndexes() method



Follow us on Facebook and Twitter for latest update.