1 for ascending 1 descending Equivalent to Select From person Order by age desc

1 for ascending 1 descending equivalent to select

This preview shows page 27 - 44 out of 53 pages.

1 for ascending; -1 descending Equivalent to: Select * From person Order by age desc What about: db.person.find().sort({name:1, age:-1}) 27
Image of page 27
Skip & limit db.person.find().limit(1) Returns the first person db.person.skip(1).limit(1) 28
Image of page 28
Distinct db.person.distinct("age") db.person.distinct("age", {age: {$gt: 20}}) distinct ages (for ages > 20) Note: MongoDB does not allow mixing distinct with find E.g., db.find(…).distinct(…) 29
Image of page 29
Distinct db.person.distinct("age").length Return # of distinct ages 30
Image of page 30
Count() db.person.count() Return # of documents in the person collection db.person.count({age: {$gt: 25}}) What does this do? db.person.find({age: {$gt: 25}}).count() 31
Image of page 31
Projection db.person.find( {"age": {$ne: 25} }, {"name":1, "age": 1} ) This will return name and age (plus _id) i.e., similar to 'select _id, name, age from users where age != 25' 32 Specify projection 1: included in result; 0: do not Specify query condition
Image of page 32
Projection This does not work: db.person.find( {"age": {$ne: 25} }, {"name":1, "age": 0} ) Can not mix 1 and 0 conditions (unless it is "_id") 33
Image of page 33
Projection db.person.find( {"age": {$ne: 25} }, {"name":1, "age": 1, "_id": 0 } ) This does not return id, e.g., { "name" : "john smith" } { "name" : "david johnson" } { "name" : "kevin small", "age" : 35 } 34
Image of page 34
Example Without projection With projection 35
Image of page 35
Update/upsert documents db.person.update( { "age": { $gt: 25 } }, { $ set : { "status": "C" } }, { multi: true } ) Similar to: Update users set status = 'C' where age > 25 Update one or all documents Existing documents may not have status field; if not, insert it instead Without $set, it will be an overwrite
Image of page 36
Another example db.person.update( {} , {$set: {"status":'C'}}, {multi:true}) Note the empty query {} Add "status" field to all documents 37
Image of page 37
Remove fields db.person.update({}, {$ unset : {"status": "" }}, {multi: true}) Remove the "status" field from all documents 38 Can put any value here
Image of page 38
Remove documents db.person.remove({}) Remove all documents/records of person db.person.remove( { "age": {$gt: 30} } ) Remove documents which satisfy a condition 39
Image of page 39
Remove a collection/table db.person.drop() This will remove the person collection/table 40
Image of page 40
Query a embedded document Using dot notation to identify field in the embedded document db.person.find({" address.city ": "LA"}) Return all documents whose city sub-field of address field = "LA" Note "" is required here for key 41
Image of page 41
Example for aggregation db.product.insert({category: "cell", store:1, qty: 10}) db.product.insert({category: "cell", store:2, qty: 20}) db.product.insert({category: "laptop", store:1, qty: 10}) db.product.insert({category: "laptop", store:2, qty: 30}) db.product.insert({category: "laptop", store:2, qty: 40}) 42
Image of page 42
Aggregation: sum db.product.aggregate({$group: {_id: "$category", total:{$sum:"$qty"}}} ) { "_id" : "laptop", "total" : 80 } { "_id" : "cell", "total" : 30 } Similar to: "select category, sum(qty) from product group by category" 43
Image of page 43
Image of page 44

You've reached the end of your free preview.

Want to read all 53 pages?

  • Fall '14

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture