0% found this document useful (1 vote)
145 views2 pages

All Mongodb Commands

This document shows various MongoDB commands for: 1. Managing databases and collections - including creating, listing, selecting, and dropping databases and collections 2. Performing CRUD operations on documents - including inserting, finding, updating, and removing documents 3. Additional document queries - including projections, limits, skips, sorts, distinct values, counts, and aggregations
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
145 views2 pages

All Mongodb Commands

This document shows various MongoDB commands for: 1. Managing databases and collections - including creating, listing, selecting, and dropping databases and collections 2. Performing CRUD operations on documents - including inserting, finding, updating, and removing documents 3. Additional document queries - including projections, limits, skips, sorts, distinct values, counts, and aggregations
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

---SHOW CURRENT DATABASE---

db
---SHOW ALL DATABASES---
show dbs
---USE A DATABASE---
use mydb
---DELETE CURRENT DATABASE---
[Link]()
---CREATE A COLLECTION IN CURRENT DATABASE---
[Link]("mycol")
[Link]("myCollection",{capped: false, autoIndexId: true})
---SHOW COLLECTIONS IN CURRENT DATABASE---
show collections
---DELETE COLLECTION---
[Link]()
---INSERT A DOCUMENT---
[Link]({name:"ahmed",age:8,count:[1,2,3,4]})
[Link]([{"_id": 1, "name": "a", "age": 5, "count":[1,2,3]},{"_id": 2, "name": "b", "age": 6,
"count": [4,5,6]}, {"_id": 3, "name": "c", "age": 7, "count": [7,8,9]}])
---SHOW ALL DOCUMENTS IN COLLECTION---
[Link]()
[Link]().pretty()
---SHOW ONE DOCUMENT IN COLLECTION---
[Link]()
[Link]().pretty()
---SAVE A DOCUMENT (UPDATES IF ID EXISTS)---
[Link]([{"_id": 1, "name": "a", "age": 5, "count":[1,2,3]},{"_id": 2, "name": "b", "age": 6,
"count": [4,5,6]}, {"_id": 3, "name": "c", "age": 7, "count": [7,8,9]}])
--SEARCH DOCUMENTS IN COLLECTION---
[Link]({age:9})
[Link]({age:{$lt:9}})
[Link]({age:{$lte:9}})
[Link]({age:{$gte:9}})
[Link]({age:{$ne:9}})
[Link]({ $and: [ {name:"row1"},{"age":9}]})
[Link]({ $or: [ {name:"row1"},{"_id":5}]})
[Link]({name:"row1","_id":5})
[Link]({name:{$in:["Ahmed","Ali"]}})
[Link]({name:{$regex:/ed/}}) // contains ed
---DELETE A DOCUMENT---
[Link]({"_id":5})
[Link]({"_id":{$gt:1}})
---UPDATE A DOCUMENT---
[Link]({name:"a"}, {$set:{age:9}})
[Link]({},{$set:{age:3}},{multi:true})
[Link]({}, {$set:{gpa:2}})
[Link]({gpa:2}, {$set:{gpa:3}})
---PROJECTION---
[Link]({},{_id:0,name:1,}).pretty()
[Link]({},{_id:0,name:0}).pretty()
---LIMIT ELEMENTS---
[Link]({},{_id:0,name:1}).limit(3)
---SKIP FIRST N ELEMENTS---
[Link]().skip(2)
---SORT---
[Link]().sort({name:1}) // ascending
[Link]().sort({name:-1}) // descending
---DISTINCT ELEMENTS---
[Link]("name")
---GET COUNT---
[Link]().count()
--AGGREGATE--
[Link]([{$match:{age:40}},{$project:{name:1}}]).pretty() // get name and id
of persons with age = 40
[Link]([{$match:{age:40}},{$project:{name:1,_id:0}}]).pretty() // get name of
persons with age = 40
[Link]([{$project:{age:1,_id:0,age2:{$multiply:["$age",2]}}}]).pretty()
[Link]([{$project:{age:1,index:1,_id:0,age2:{$multiply:["$age","$index"]}}}]).p
retty()
[Link]([{$match:{age:{$lt:40}}},{$project:{name:1}}])
[Link]([{$project:{_id:0, "company":1}},{$count:"count is"}])
[Link]([{$match: {age:{$ne:40}}},{$project:{_id:0, "company":1}},{$limit:5}])
[Link]([{$match: {age:{$ne:40}}},{$skip:3},{$limit:3}]).pretty()
[Link]([{$match:{age:40}},{$skip:3}, {$limit:2},{$sort:{name:1}}]).pretty()
[Link]([{$match:{age:40}},{$skip:3}, {$limit:2},{$sort:{name:1}},
{$out:"result"}]).pretty() // create new collection with name "result"
[Link]([{$match:{age:40}},{$skip:3}, {$limit:2},{$sort:{name:1}}]).itcount()
[Link]([{$match:{age:40}},{$skip:3}, {$limit:2},{$sort:{name:1}}]).toArray()
[Link]([{$match:{age:40}},{$skip:3},
{$limit:2},{$sort:{name:1}}]).toArray().length
[Link]([{$match:{tags:{$size:3}}}])
[Link]([{$project:{name:1, tags:1, _id:0}}, {$unwind:"$tags"}]) // repeat for
each element in tags
[Link]([{$group:{_id:"$gender"}}])
[Link]([{$group:{_id:{ec:"$eyeColor",g:"$gender"}}}])
[Link]([{$group:{_id:"$[Link]"}}, {$sort:{_id:-1}}])

You might also like