Features of NoSQL
Installing mongoDB
For Installation instructions, visit:
https://www.mongodb.com/docs/manual/administration/install-community/
Open the MongoDB configuration file in your preferred text editor. The following example uses nano:
sudo nano /etc/mongod.conf
Append a comma to this line followed by your MongoDB server’s public IP address:
/etc/mongod.conf
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1,mongodb_server_ip
Please note that this should be the IP address of the server on which you’ve installed MongoDB.
Then, restart MongoDB to put this change into effect:
sudo systemctl restart mongod
Note: When you open the mongoshell, you can create a database by using this syntax:
use <database_name>
Examples of MongoDB Queries
Inserting Values
db.cars.insertOne(
{ model:"Chrysler 300",engine:[6,8]}
)
db.cars.insertMany([
{ model:"Dodge Challenger",variant:"SXT",colours:["red","green","blue"]},
{ model:"Dodge Challenger",variant:"RT",colours:["red","yellow","blue"],special:true},
{ model:"Dodge Charger",variant:"RT",colours:["blue","green","black"],tires:[18,19],special:true}
])
Update:
db.cars.updateOne(
{ model: "Chrysler 300" },
{
$push: { seater: 4},
$currentDate: { lastModified: true }
}
)
db.cars.updateMany(
{ model: "Dodge Challenger" },
{
$push: { seater: 4}
}
)
db.cars.updateMany(
{ model: "Dodge Challenger" },
{
$set: { seater: 2}
}
)
Find out which cars are special models.
db.cars.find(
{special:true}
)
Find out which cars have a blue, green and black colour option. Make sure that no other options are available other than these colours.
db.cars.find({
colours:["blue","green","black"]
})
Find out which cars have a red or blue colour option.
db.cars.find({
$or:[{colours:"red"},{colours:"blue"}]
})
Using the or operator, print out any of these two car models:- Dodge Challenger and Chrysler 300
db.cars.find( {
$or: [{model:"Dodge Challenger"},{model:"Chrysler 300"}]
}
)
Find which cars have more than 2 seats.
db.cars.find( {
seater:{$gt:2}
}
)