Mongo DB ReadWrite Agenda Query Interface Query Behavior
Mongo. DB Read/Write
Agenda • Query Interface • Query Behavior • Query Statements • Insert • Update • Delete
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
Read Operations Mongo. DB
Read Operations Mysql
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.
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()
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.
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()
Nested Fields • Use “dot notation” • db. restaurants. find( { "address. zipcode": "10075" } ) • db. restaurants. find( { "grades. score": { $gt: 30 } } )
Query Behavior • All queries in Mongo. DB 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.
How Query Statements Work
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.
Projections
Projection Examples db. restaurants. find({"cuisine": "Bakery"}, {"name": 1, "address": 1 }). pretty() db. restaurants. find({"cuisine": "Bakery"}, {"address": 0, "grades": 0 }). pretty() db. restaurants. find({"cuisine": "Bakery"}, {"name": 1, "address": 1, "_id": 0 }). pretty()
- Slides: 15