Download presentation
Presentation is loading. Please wait.
1
MongoDB Read
2
Agenda Query Interface Query Behavior Query Statements
3
Read operations Queries select documents from a single collection.
db.collection.find() Find() accepts the query criteria (what to return) It returns a cursor to the document set Cursor points to the first 20 docs in the result set, is iterable. Limits can be applied. Projections return only wanted fields Limit will limit the number of documents returned
4
Read Operations MongoDB
5
Read Operations Mysql
6
Query Selectors (Comparisons)
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.
7
Query Selectors (Comparisons)
Name Description $eq db.restaurants.find({"address.building": {$eq: "195"}}).pretty() $gt db.restaurants.find({"address.zipcode": {$gt: "10020"}}).pretty() $gte db.restaurants.find({"address.zipcode": {$gte: "10020"}}).pretty() $lt db.restaurants.find({"address.zipcode": {$lt: "10045"}}).pretty() $lte db.restaurants.find({"address.zipcode": {$lte: "10020"}}).pretty() $ne db.restaurants.find({"borough": {$ne: "Manhattan"}}).pretty() $in db.restaurants.find({"address.zipcode": {$in: ["10020", "10021", "10022"]}}).pretty() $nin db.restaurants.find({"address.zipcode": {$nin: ["10020", "10021", "10022"]}}).pretty()
8
Query Selectors (Logical)
Name Description $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.
9
Query Selectors (Logical)
Name Description $or db.restaurants.find({ $or: [{"cuisine": "Italian"}, {"address.zipcode": "10075"}]}).pretty() $and db.restaurants.find({ $and: [{"cuisine": "Italian"}, {"address.zipcode": "10075"}]}).pretty() $not db.restaurants.find({"cuisine": {$not: {$eq: "Italian"}}}).pretty()
10
Query Behavior All queries in MongoDB refer to a single collection (Ex. restaurants) Modify the results via: Limits, Skips, Sort Orders The order of the documents can be random unless you specify a sort() order. Updates also use the same syntax as Reads to select documents to update.
11
How Query Statements Work
12
Projections Queries will automatically return all fields in all matching docs by default. Limit the number of fields returned by each document with a projection Projections are the second argument in the find() method. Can specify a list of fields to return Can also specify a list of fields to exclude You cannot mix inclusive and exclusive projections together. Except for excluding the _id field in an inclusive projection.
13
Projections
14
Projection Examples db.restaurants.find({"cuisine": "Bakery"},
{"name": 1, "address": 1 }).pretty() {"address": 0, "grades": 0 {"name": 1, "address": 1, "_id": 0
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.