MongoDB provides various methods for querying data. Note that they are case-sensitive and need to be called exactly as written below
Query Method | Description |
---|---|
db.collection.find() | Performs a query on a collection and returns a cursor object. |
db.collection.findAndModify() | Atomically modifies and returns a single document. |
db.collection.findOne() | Performs a query and returns a single document ... the first one found by natural search order. |
db.collection.findOneAndDelete() | Finds a single document and deletes it. |
db.collection.findOneAndReplace() | Finds a single document and replaces it. |
db.collection.findOneAndUpdate() | Finds a single document and updates it. |
db.collection.find( <filter>, <projection> )
This is the basic method of querying data.
Filter and Projection are optional documents that modify the query.
Filter uses key:value pairs to restrict the Documents returned and Projection uses flagged fields to restrict the Fields returned (aka projected)
db.clients.find({ name: { last: "Musterman", first: "Max" }})
This will return all the Documents from the clients Collection with first name Max and last name Musterman
db.orders.find({ value $gt 1000 }, { company: 1, _id: 0})
This will return just the company Field for all orders valued over 1000 in the orders Collection. 1 switches a column on and 0 switches it off. The _id field must be specifically excluded
Various operators can be used. ':' means equality, but we can also use $eq
Filter and Projection are optional documents that modify the query.
Filter uses key:value pairs to restrict the Documents returned and Projection uses flagged fields to restrict the Fields returned (aka projected)
db.clients.find({ name: { last: "Musterman", first: "Max" }})
This will return all the Documents from the clients Collection with first name Max and last name Musterman
db.orders.find({ value $gt 1000 }, { company: 1, _id: 0})
This will return just the company Field for all orders valued over 1000 in the orders Collection. 1 switches a column on and 0 switches it off. The _id field must be specifically excluded
Various operators can be used. ':' means equality, but we can also use $eq
Name | Description |
---|---|
$eq | Matches values that are equal to a specified value. |
$gt | Matches values that are greater than a specified value. |
$gte | Matches values that are greater than or equal to a specified value. |
$lt | Matches values that are less than a specified value. |
$lte | Matches values that are less than or equal to a specified value. |
$ne | Matches all values that are not equal to a specified value. |
$in | Matches any of the values specified in an array. |
$nin | Matches none of the values specified in an array. |
$or | Joins query clauses with a logical OR returns all documents that match the conditions of either clause. |
$and | Joins query clauses with a logical AND returns all documents that match the conditions of both clauses. |
$not | Inverts the effect of a query expression and returns documents that do not match the query expression. |
$nor | Joins query clauses with a logical NOR returns all documents that fail to match both clauses. |
$exists | Matches documents that have the specified field. |
$type | Selects documents if a field is of the specified type. |
$mod | Performs a modulo operation on the value of a field and selects documents with a specified result. |
$regex | Selects documents where values match a specified regular expression. |
$text | Performs text search. |
$where | Matches documents that satisfy a JavaScript expression. |
$geoWithin | Selects geometries within a bounding GeoJSON geometry. The 2dsphere and 2d indexes support $geoWithin. |
$geoIntersects | Selects geometries that intersect with a GeoJSON geometry. The 2dsphere index supports $geoIntersects. |
$near | Returns geospatial objects in proximity to a point. Requires a geospatial index. The 2dsphere and 2d indexes support $near. |
$nearSphere | Returns geospatial objects in proximity to a point on a sphere. Requires a geospatial index. The 2dsphere and 2d indexes support $nearSphere. |
$all | Matches arrays that contain all elements specified in the query. |
$elemMatch | Selects documents if element in the array field matches all the specified $elemMatch conditions. |
$size | Selects documents if the array field is a specified size. |
$bitsAllSet | Matches numeric or binary values in which a set of bit positions all have a value of 1. |
$bitsAnySet | Matches numeric or binary values in which any bit from a set of bit positions has a value of 1. |
$bitsAllClear | Matches numeric or binary values in which a set of bit positions all have a value of 0. |
$bitsAnyClear | Matches numeric or binary values in which any bit from a set of bit positions has a value of 0. |
$comment | Adds a comment to a query predicate. |
$ | Projects the first element in an array that matches the query condition. |
$elemMatch | Projects the first element in an array that matches the specified $elemMatch condition. |
$meta | Projects the document’s score assigned during $text operation. |
$slice | Limits the number of elements projected from an array. Supports skip and limit slices. |
Output of find is a cursor, which can be modified with various additional methods.
For instance,
db.collection.find().pretty
will return the cursor results in an easy-to-read format
Nice sir,Thanks for your sharing information,good article it helps many students.for more information please visit our website.
ReplyDeleteMongoDB Training in Hyderabad
Nice post. Thank you.
ReplyDeleteCCNA training in Bangalore provides basic to advance level training following the CISCO –CCNA global training module. After completion of our CCNA training in Bangalore one could easily apply for jobs available in different MNCs. ICSS is the best institute in Bangalore.
https://indiancybersecuritysolutions.com/ccna-training-in-bangalore/
As the growth of Big data solutions companies , it is essential to spread knowledge in people. This meetup will work as a burst of awareness.
ReplyDeletehanks for sharing amazing information.Gain the knowledge and hands-on experience
ReplyDeleteMongodb Training in Bangalore