0% found this document useful (0 votes)
61 views

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 (0 votes)
61 views

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
You are on page 1/ 2

---SHOW CURRENT DATABASE---

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

You might also like