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.
Setting Up
First, select the database that we used in the previous section, called 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:
MongoDBdb.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:
MongoDBdb.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:
MongoDBdb.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:
MongoDBdb.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:
MongoDBdb.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:
MongoDBdb.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:
MongoDBdb.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:
MongoDBdb.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: '...' }
}
]
MongoDBdb.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:
MongoDBdb.users.find({ age: { $gte: 5 } }).count()
Example Output
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:
MongoDBdb.users.find({
$or: [
{ username: "test2" },
{ username: "test4" }
]
})
Example Output
[
{
_id: ObjectId("62ab271b1d70abe84b6d27d8"),
username: 'test4',
age: 22,
hobbies: [ 'skiing', 'walking' ],
address: { street: '...', zipcode: '...' }
}
]
MongoDBdb.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:
MongoDBdb.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:
MongoDBdb.users.find( { hobbies: { "$in" : ["walking"]} } );
Example Output
[
{
_id: ObjectId("62ab271b1d70abe84b6d27d8"),
username: 'test4',
age: 22,
hobbies: [ 'skiing', 'walking' ],
address: { street: '...', zipcode: '...' }
}
]