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

Basic Commands in MongoDB

Uploaded by

Shreya kumar
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)
21 views

Basic Commands in MongoDB

Uploaded by

Shreya kumar
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/ 4

Mongo DB commands

1. Starting Mongo DB
omkar@omkar:~$ mongo
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
2019-08-14T20:21:20.434+0530 I CONTROL [initandlisten]

2. use
> use mydb
switched to db mydb

3. db
> db
mydb

4. showdbs
> show dbs
admin 0.000GB
local 0.000GB
mydb 0.000GB

5. createCollection
> db.createCollection("institute")
{ "ok" : 1 }

6. show collections
> show collections
institute
myCollections
student

7. insert
> db.myCollections.insert({"name":"Omkar"})
WriteResult({ "nInserted" : 1 })
> db.myCollections.insert({"name":"Bhargavi"})
WriteResult({ "nInserted" : 1 })
> db.myCollections.insert({"name":"Sabrish",college:"SIES"})
WriteResult({ "nInserted" : 1 })

8. find
> db.myCollections.find()
{ "_id" : ObjectId("5d542122588fffb47c59e48b"), "name" : "Bhargavi" }
{ "_id" : ObjectId("5d5421ca588fffb47c59e48d"), "college" : "RAIT" }
{ "_id" : ObjectId("5d5421cf588fffb47c59e48e"), "name" : "Sabrish", "college" : "SIES" }

9. update
> db.myCollections.update({"name":"Omkar"},{$set:{"college":"RAIT"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
10. pretty
> db.myCollections.find().pretty()
{
"_id" : ObjectId("5d542122588fffb47c59e48b"),
"name" : "Omkar",
"college" : "RAIT"
}
{ "_id" : ObjectId("5d5421ca588fffb47c59e48d"), "name" : "Bhargavi" }
{
"_id" : ObjectId("5d5421cf588fffb47c59e48e"),
"name" : "Sabrish",
"college" : "SIES"
}

11. save
> db.myCollections.save({"name":"Amey",college:"Terana"})
WriteResult({ "nInserted" : 1 })
> db.myCollections.find().pretty()
{
"_id" : ObjectId("5d542122588fffb47c59e48b"),
"name" : "Omkar",
"college" : "RAIT"
}
{ "_id" : ObjectId("5d5421ca588fffb47c59e48d"), "name" : "Bhargavi" }
{
"_id" : ObjectId("5d5421cf588fffb47c59e48e"),
"name" : "Sabrish",
"college" : "SIES"
}
{
"_id" : ObjectId("5d542b2a588fffb47c59e48f"),
"name" : "Amey",
"college" : "Terana"
}

12. remove
> db.myCollections.remove({"name":"Sabrish"})
WriteResult({ "nRemoved" : 1 })
> db.myCollections.find().pretty()
{
"_id" : ObjectId("5d542f9691b650560c6b7efe"),
"name" : "Omkar",
"college" : "RAIT"
}
{ "_id" : ObjectId("5d542fa091b650560c6b7eff"), "name" : "Bhargavi" }
{
"_id" : ObjectId("5d542fe191b650560c6b7f01"),
"name" : "Amey",
"college" : "Terana"
}
13. sort (Ascending)
> db.myCollections.find().sort({"name":1})
{ "_id" : ObjectId("5d542b2a588fffb47c59e48f"), "name" : "Amey", "college" : "Terana" }
{ "_id" : ObjectId("5d5421ca588fffb47c59e48d"), "name" : "Bhargavi" }
{ "_id" : ObjectId("5d542122588fffb47c59e48b"), "name" : "Omkar", "college" : "RAIT" }

14. sort (Descending)


> db.myCollections.find().sort({"name":-1})
{ "_id" : ObjectId("5d542122588fffb47c59e48b"), "name" : "Omkar", "college" : "RAIT" }
{ "_id" : ObjectId("5d5421ca588fffb47c59e48d"), "name" : "Bhargavi" }
{ "_id" : ObjectId("5d542b2a588fffb47c59e48f"), "name" : "Amey", "college" : "Terana" }

15. count
> db.myCollections.find().count()
3

16. limit
> db.myCollections.find().limit(2)
{ "_id" : ObjectId("5d542122588fffb47c59e48b"), "name" : "Omkar", "college" : "RAIT" }
{ "_id" : ObjectId("5d5421ca588fffb47c59e48d"), "name" : "Bhargavi" }

17. dataSize
> db.myCollections.dataSize()
154

18. storageSize
> db.myCollections.storageSize()
36864

19. totalSize
> db.myCollections.totalSize()
73728

20. totalIndexSize
> db.myCollections.totalIndexSize()
36864

21. renameCollection
> db.myCollections.renameCollection("newCollection")
{ "ok" : 1 }
> show collections
institute
newCollection
student
22. copyTo
> db.createCollection("collection2")
{ "ok" : 1 }
> db.newCollection.copyTo("collection2")
WARNING: db.eval is deprecated
3

> db.collection2.find()
{ "_id" : ObjectId("5d542122588fffb47c59e48b"), "name" : "Omkar", "college" : "RAIT" }
{ "_id" : ObjectId("5d5421ca588fffb47c59e48d"), "name" : "Bhargavi" }
{ "_id" : ObjectId("5d542b2a588fffb47c59e48f"), "name" : "Amey", "college" : "Terana" }

23. getIndexes
> db.collection2.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mydb.collection2"
}
]

24. isCapped
> db.collection2.isCapped()
false

25. aggregate
>db.collection2.update({"name":"Bhargavi"},{$set:{"college":"RAIT"}})
> db.collection2.find()
{ "_id" : ObjectId("5d542122588fffb47c59e48b"), "name" : "Omkar", "college" : "RAIT" }
{ "_id" : ObjectId("5d5421ca588fffb47c59e48d"), "name" : "Bhargavi", "college" : "RAIT" }
{ "_id" : ObjectId("5d542b2a588fffb47c59e48f"), "name" : "Amey", "college" : "Terana" }

> db.collection2.aggregate([{$match:{college:"RAIT"}}])

{ "_id" : ObjectId("5d542122588fffb47c59e48b"), "name" : "Omkar", "college" : "RAIT" }


{ "_id" : ObjectId("5d5421ca588fffb47c59e48d"), "name" : "Bhargavi", "college" : "RAIT" }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy