MongoDB Lab
MongoDB Lab
Install MongoDB:
https://www.mongodb.com/try/download/community
Install MongoDB tools:
https://www.mongodb.com/try/download/database-tools?tck=docs_dat
abasetools
In the path C:\Program Files\MongoDB\Server\5.0\bin , run
mongo.exe
Use Program Files\MongoDB\Server\5.0\bin>mongoimport -d rest -c
rest_col --file restaurants.json to import sample database
MongoDB commands
C:\Program Files\MongoDB\Server\5.0\bin
show dbs;
use lab;
show collections;
db.student.insert({roll:"11",name:"Shyam"});
db.student.insert({roll:"12"});
db.student.insert({roll:"13",name:"Sita",Mobile:"985555"});
db.student.find();
db.student.find({roll:{$gt:"12"}});
db.student.insert({roll:"30", phone:["984188888","9851099999"]});
https://www.mongodb.com/docs/manual/tutorial/project-fields-from-query-results/
commands
1. use DATABASE_NAME
2. show dbs
3. db.movie.insert({"name":"tutorials point"})
4. db.dropDatabase()
5. db.createCollection("mycollection")
{ "ok" : 1 }
6. db.createCollection("mycol", { capped : true, autoIndexID : true, size : 6142800, max : 10000 } ){
"ok" : 0,
"errmsg" : "BSON field 'create.autoIndexID' is an unknown field.",
"code" : 40415,
"codeName" : "Location40415"
}
db.createCollection("post")
> db.post.insert([
{
title: "MongoDB Overview",
description: "MongoDB is no SQL database",
by: "tutorials point",
url: "http://www.tutorialspoint.com",
tags: ["mongodb", "database", "NoSQL"],
likes: 100
},
{
title: "NoSQL Database",
description: "NoSQL database doesn't have tables",
by: "tutorials point",
url: "http://www.tutorialspoint.com",
tags: ["mongodb", "database", "NoSQL"],
likes: 20,
comments: [
{
user:"user1",
message: "My first comment",
dateCreated: new Date(2013,11,10,2,35),
like: 0
}
]
}
])
BulkWriteResult({ "writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
Lab Solutions
1. db.restaurants.find();
2. db.restaurants.find({},{"restaurant_id" : 1,"name":1,"borough":1,"cuisine" :1});
3. db.restaurants.find({},{"restaurant_id" : 1,"name":1,"borough":1,"cuisine" :1,"_id":0});
4. db.restaurants.find({},{"restaurant_id" :
1,"name":1,"borough":1,"address.zipcode" :1,"_id":0});
5. db.restaurants.find({"borough": "Bronx"});
6. db.restaurants.find({"borough": "Bronx"}).limit(5);
7. db.restaurants.find({"borough": "Bronx"}).skip(5).limit(5);
8. db.restaurants.find({grades : { $elemMatch:{"score":{$gt : 90}}}});
Source: https://www.w3resource.com/mongodb-exercises/#PracticeOnline