Skip to content

Finding Documents

Finding Documents

To find documents, the commands db.collection.find() and db.collection.findOne() can be used. The latter command only returns back one result.

MongoDB
db.users.find({})

Setting Up

First, select the database that we used in the previous section, called test:

MongoDB
use test;

In this database you should have the collection users.

Specifying Fields

To further specify what we want to search for, we can pass in fields that we want to search for:

MongoDB
db.users.find({ username: "test4" })
Example Output
[
  {
    _id: ObjectId("62ab271b1d70abe84b6d27d8"),
    username: 'test4',
    age: 22,
    hobbies: [ 'skiing', 'walking' ],
    address: { street: '...', zipcode: '...' }
  }
]

And to only get one result back, the findOne can be used instead:

MongoDB
db.users.findOne({ username: "test4" })
Example Output
{
  _id: ObjectId("62ab271b1d70abe84b6d27d8"),
  username: 'test4',
  age: 22,
  hobbies: [ 'skiing', 'walking' ],
  address: { street: '...', zipcode: '...' }
}

Multiple fields can also be added to form an implicit AND statement:

MongoDB
db.users.findOne({ username: "test4", age: 22 })
Example Output
{
  _id: ObjectId("62ab271b1d70abe84b6d27d8"),
  username: 'test4',
  age: 22,
  hobbies: [ 'skiing', 'walking' ],
  address: { street: '...', zipcode: '...' }
}

Limiting Results

To limit how many results will be coming back, limit can be used.

For example, to get all users and limit the amount of results to 2:

MongoDB
db.users.find({}).limit(2)
Example Output
[
  {
    _id: ObjectId("62aae293adec45e9cb477b3b"),
    username: 'tester',
    password: 'abc123',
    age: 19
  },
  {
    _id: ObjectId("62ab271b1d70abe84b6d27d8"),
    username: 'test4',
    age: 22,
    hobbies: [ 'skiing', 'walking' ],
    address: { street: '...', zipcode: '...' }
  }
]

Using LIKE

To find users that have for example name like test, this kind of syntax can be used:

MongoDB
db.users.find({ username: /test/ })
Example Output
[
  {
    _id: ObjectId("62aae293adec45e9cb477b3b"),
    username: 'tester',
    password: 'abc123',
    age: 19
  },
  {
    _id: ObjectId("62ab271b1d70abe84b6d27d8"),
    username: 'test4',
    age: 22,
    hobbies: [ 'skiing', 'walking' ],
    address: { street: '...', zipcode: '...' }
  }
]

Finding Less and More Than

To find something something that has less than or more than, the commands below can be used:

  • \$lt: Finds all values less than the given value
  • \$lte: Finds all values less than or exactly the given value
  • \$gt: Finds all values greater than the given value
  • \$gte: Finds all values greater than or exactly the given value

For example to find all users with the age less than 23:

MongoDB
db.users.find({ age: { $lt: 23 } })
Example Output
[
  {
    _id: ObjectId("62aae293adec45e9cb477b3b"),
    username: 'tester',
    password: 'abc123',
    age: 19
  },
  {
    _id: ObjectId("62ab271b1d70abe84b6d27d8"),
    username: 'test4',
    age: 22,
    hobbies: [ 'skiing', 'walking' ],
    address: { street: '...', zipcode: '...' }
  }
]

And to find all users with age above or exactly 5:

MongoDB
db.users.find({ age: { $gte: 5 } })
Example Output
[
  {
    _id: ObjectId("62aae293adec45e9cb477b3b"),
    username: 'tester',
    password: 'abc123',
    age: 19
  },
  {
    _id: ObjectId("62ab271b1d70abe84b6d27d8"),
    username: 'test4',
    age: 22,
    hobbies: [ 'skiing', 'walking' ],
    address: { street: '...', zipcode: '...' }
  }
]

Sorting Results

To sort the results coming back, sort can be used.

For example:

MongoDB
db.users.find({}).limit(2).sort( { age: 1 } )
Example Output
[
  {
    _id: ObjectId("62aae293adec45e9cb477b3b"),
    username: 'tester',
    password: 'abc123',
    age: 19
  },
  {
    _id: ObjectId("62ab271b1d70abe84b6d27d8"),
    username: 'test4',
    age: 22,
    hobbies: [ 'skiing', 'walking' ],
    address: { street: '...', zipcode: '...' }
  }
]
MongoDB
db.users.find({}).limit(2).sort( { age: -1 } )
Example Output
[
  {
    _id: ObjectId("62ab271b1d70abe84b6d27d8"),
    username: 'test4',
    age: 22,
    hobbies: [ 'skiing', 'walking' ],
    address: { street: '...', zipcode: '...' }
  },
  {
    _id: ObjectId("62aae293adec45e9cb477b3b"),
    username: 'tester',
    password: 'abc123',
    age: 19
  }
]

Counting Results

To count the results that we are getting back, only .count() will need to added to the end of the call.

For example:

MongoDB
db.users.find({ age: { $gte: 5 } }).count()
Example Output
2

AND and OR

It is also possible to use the AND and OR words to further filter the results.

This can be achieved by using $and and \$or

For example:

MongoDB
db.users.find({
  $or: [
    { username: "test2" },
    { username: "test4" }
  ]
})
Example Output
[
  {
    _id: ObjectId("62ab271b1d70abe84b6d27d8"),
    username: 'test4',
    age: 22,
    hobbies: [ 'skiing', 'walking' ],
    address: { street: '...', zipcode: '...' }
  }
]
MongoDB
db.users.find({
  $and: [
    { username: "test2" },
    { age: 21 }
  ]
})
Example Output
[
  {
    _id: ObjectId("62ab28ab1d70abe84b6d27d9"),
    username: 'test2',
    age: 21
  }
]

Finding from Child Objects

To find something from objects, the dot . character can be used to separate each object:

MongoDB
db.users.findOne({ "address.street": "..." })
Example Output
[
  {
    _id: ObjectId("62ab271b1d70abe84b6d27d8"),
    username: 'test4',
    age: 22,
    hobbies: [ 'skiing', 'walking' ],
    address: { street: '...', zipcode: '...' }
  }
]

Finding from Child Arrays

To find something from arrays, this kind of syntax can be used:

MongoDB
db.users.find( { hobbies: { "$in" : ["walking"]} } );
Example Output
[
  {
    _id: ObjectId("62ab271b1d70abe84b6d27d8"),
    username: 'test4',
    age: 22,
    hobbies: [ 'skiing', 'walking' ],
    address: { street: '...', zipcode: '...' }
  }
]