Mongo DB Mongo DB Commands Start the server

Mongo. DB

Mongo. DB Commands Start the server mongod Start the client Mongo Create a database called “ca” use ca Create a collection called “training db. training. insert({}) Insert five documents ({"name": "marker", "color": "black", "make": "ca mlin", 'qty': 8}) db. training. insert({"name": "marker 1", "color": "black", "make": "camlin", 'qty': 9}) db. training. insert({"name": "marker 2", "color" : "black", "make": "camlin", 'qty': 10}) db. training. insert({"name": "marker 3", "color" : "black", "make": "camlin", 'qty': 11}) db. training. insert({"name": "marker 4", "color" : "black", "make": "camlin", 'qty': 12})

Mongo. DB Commands List all the documents db. training. find() Count the documents inserted db. training. count() Create a document with custom_id db. training. insert({"custom_id": 1}) List all databases show dbs List all the collections for the current db show collections Command to check mongodb version db. version() List all the commands in help() list db. help()

Mongo. DB Commands Selects all the documents in the collection db. training. find({}) Find all the documents with name=marker 1 db. training. find({“name”: ”marker 1”}) Projection command to specify the inclusion of the field <field>: <1 or true> Projection command to specify the exclusion of the field <field>: <0 or false> Command to remove name with marker 1 db. training. remove({“name”: ”marker 1”})

Mongo. DB Commands Command to delete only first record if there are multiple records db. training. remove(({“name”: ”marker 1”}, 1) Command to delete all db. training. remove() documents from the collection Put a file into mongodb Grid. FS using put mongofiles -d database 1 put abc. mpg List all files in a db using list mongofiles -d database 1 list Get a file using get mongofiles -d database 1 get abc. mpg

Mongo. DB Commands Delete a file using delete mongofiles -d database 1 delete abc. mpg Creates an index on the specified field db. coll. ensure. Index({“title”: 1}) Creates index on db. coll. ensure. Index({“title”: 1, ”description”: db. coll. ensure. Index({“title”: 1}) multiple fields 1}) Documents displays in db. coll. find({“title”: 1, _id: 0}). sort({“title”: -1}) descending order Drop database use mydb db. drop. Database() Drop Collection db. coll. drop() Displays only 2 documents while querying the document db. coll. find({}, {“title”: 1, _id: 0}). limit(2)

Mongo. DB Commands Delete a file using delete mongofiles -d database 1 delete abc. mpg Creates an index on the specified field db. coll. ensure. Index({“title”: 1}) Creates index on db. coll. ensure. Index({“title”: 1, ”description”: db. coll. ensure. Index({“title”: 1}) multiple fields 1}) Documents displays in db. coll. find({“title”: 1, _id: 0}). sort({“title”: -1}) descending order Drop database use mydb db. drop. Database() Drop Collection db. coll. drop()

Mongo. DB Commands Displays only 2 documents while querying the document db. coll. find({}, {“title”: 1, _id: 0}). limit(2) Display only second document db. coll. find({}), {“title”: 1, _id: 0}). skip(2) Note: Default value in skip() method is 0
- Slides: 8