Professional Documents
Culture Documents
{sid:22,sname:'dustin',rating:7,age:45},{sid:29,sname:'brutus',rating:1,age:33},
{sid:31,sname:'lubber',rating:8,age:55},{sid:32,sname:'andy',rating:8,age:25.5},
{sid:58,sname:'rusty',rating:10,age:35},{sid:64,sname:'horatio',rating:7,age:35},
{sid:71,sname:'zorba',rating:7,age:16},{sid:74,sname:'horatio',rating:9,age:40},
{sid:85,sname:'art',rating:3,age:25.5},{sid:95,sname:'bob',rating:3,age:63.5}
])
db.sailors.find()
OUTPUT:
OUTPUT :
admin 0.000GB
db 0.000GB
local 0.000GB
test 0.000GB
db.boats.insert({bid:101,bname:'interlake',color:'blue'});
db.boats.insertMany([
{bid:102,bname:'interlake',color:'red'},
{bid:103,bname:'clipper',color:'green'},
{bid:104,bname:'marine',color:'red'}
]);
> db.boats.find().pretty();
{
"_id" : ObjectId("5d986aab44e2d81638842a9d"),
"bid" : 101,
"bname" : "interlake",
"color" : "blue"
"_id" : ObjectId("5d986b7244e2d81638842a9e"),
"bid" : 102,
"bname" : "interlake",
"color" : "red"
"_id" : ObjectId("5d986b7244e2d81638842a9f"),
"bid" : 103,
"bname" : "clipper",
"color" : "green"
"_id" : ObjectId("5d986b7244e2d81638842aa0"),
"bid" : 104,
"bname" : "marine",
"color" : "red"
}
#inserting rows
db.reserves.insertMany([
{sid:22,bid:102,day:'1998-10-10'},{sid:22,bid:103,day:'1998-10-8'},
{sid:22,bid:104,date:'1998-10-7'},{sid:31,bid:102,date:'1998-11-10'},
{sid:31,bid:103,date:'1998-11-6'},{sid:31,bid:104,date:'1998-11-12'},
{sid:64,sname:101,date:'1998-9-5'},{sid:64,bid:102,date:1998-9-8},
{sid:74,bid:103,date:'1998-9-8'}
]);
> db.reserves.find();
output
output
boats
reserves
sailors
>db.sailors.update({sid:22},{sid:22,sname:'Dustin',rating:7,age:45});
output
output
>db.sailors.updateOne({sid:22},{$set: {sname:'Dustin',rating:7,age:45}});
db.sailors.updateMany({sid:22},{$set:{age:20}});
output
db.sailors.update({sid:95},{$push:{nickname:'boo'}});
output
db.sailors.update({sid:22},{$pop:{nickname:1}});
#update $pull
db.sailors.update({sid:22},{$pull:{nickname:'dust'}});
output
db.sailors.update({sid:22},{$pop:{nickname:1}});
output
output
"_id" : ObjectId("5d986f0744e2d81638842aa5"),
"sid" : 31,
"bid" : 102,
"date" : "1998-11-10"
> db.reserves.find({sid:31});
output
#syntax
>db.collection.find.sort({key: 1 or -1});
Example:
db.sailors.find().sort({sid:1});
> db.sailors.find().sort({sid:-1});
output
db.sailors.find().count();
10
#and
>db.sailors.find({$and:[{sid:22},{rating:7}]}).pretty();
"_id" : ObjectId("5d9868d644e2d81638842a9b"),
"sid" : 22,
"sname" : "Dustin",
"rating" : 7,
"age" : 29,
"nickname" : [ ]
#$or
db.sailors.find({$or:[{sid:22},{rating:7}]}).pretty();
"_id" : ObjectId("5d98671644e2d81638842a96"),
"sid" : 64,
"sname" : "horatio",
"rating" : 7,
"age" : 35
"_id" : ObjectId("5d98671644e2d81638842a97"),
"sid" : 71,
"sname" : "zorba",
"rating" : 7,
"age" : 16
"_id" : ObjectId("5d9868d644e2d81638842a9b"),
"sid" : 22,
"sname" : "Dustin",
"rating" : 7,
"age" : 29,
"nickname" : [ ]
}
#not
db.sailors.find().skip(2);
>
db.sailors.find().count();
10
> db.boats.find().count();
db.sailors.find().limit(2);
>
# condition operations
#$eq, $gt,$lt,$gte,$lte
db.sailors.find({sid:{$eq:22}});
{ "_id" : ObjectId("5d9868d644e2d81638842a9b"), "sid" : 22, "sname" : "Dustin", "rating" : 7, "age" : 29,
"nickname" : [ ] }
>
#$lt--less than
db.sailors.find({rating:{$lt:7}});
> db.sailors.find({rating:{$gt:7}});
> db.sailors.find({rating:{$gte:7}});
> db.sailors.find({rating:{$lte:7}});
>
#deletion
#deleteOne()
> db.sailors.deleteOne({rating:7});
{ "acknowledged" : true, "deletedCount" : 1 }
#deleteMany()
db.sailors.deleteMany({rating:7});
#remove()
db.sailors.remove({sid:95});
WriteResult({ "nRemoved" : 1 })
#update $push--- creates a new coloumn
db.sailors.update({sid:95},{$push:{nickname:'boo'}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
db.sailors.find();
{ "_id" : ObjectId("5d98671644e2d81638842a93"), "sid" : 31, "sname" : "lubber", "rating" : 8, "age" : 55 }
{ "_id" : ObjectId("5d98671644e2d81638842a94"), "sid" : 32, "sname" : "andy", "rating" : 8, "age" : 25.5 }
{ "_id" : ObjectId("5d98671644e2d81638842a95"), "sid" : 58, "sname" : "rusty", "rating" : 10, "age" : 35 }
{ "_id" : ObjectId("5d98671644e2d81638842a96"), "sid" : 64, "sname" : "horatio", "rating" : 7, "age" : 35 }
{ "_id" : ObjectId("5d98671644e2d81638842a97"), "sid" : 71, "sname" : "zorba", "rating" : 7, "age" : 16 }
{ "_id" : ObjectId("5d98671644e2d81638842a98"), "sid" : 74, "sname" : "horatio", "rating" : 9, "age" : 40 }
{ "_id" : ObjectId("5d98671644e2d81638842a99"), "sid" : 85, "sname" : "art", "rating" : 3, "age" : 25.5 }
{ "_id" : ObjectId("5d98671644e2d81638842a9a"), "sid" : 95, "sname" : "bob", "rating" : 3, "age" : 63.5,
"nickname" : [ "boo" ] }
{ "_id" : ObjectId("5d9868d644e2d81638842a9b"), "sid" : 22, "sname" : "Dustin", "rating" : 7, "age" : 29,
"nickname" : ["dust" ] }
{ "_id" : ObjectId("5d9868d644e2d81638842a9c"), "sid" : 29, "sname" : "brutus", "rating" : 1, "age" : 33,
"nickname" : [ "bru", "bru" ] }
>
> db.sailors.find();
{ "_id" : ObjectId("5d98671644e2d81638842a93"), "sid" : 31, "sname" : "lubber", "rating" : 8, "age" : 55 }
{ "_id" : ObjectId("5d98671644e2d81638842a94"), "sid" : 32, "sname" : "andy", "rating" : 8, "age" : 25.5 }
{ "_id" : ObjectId("5d98671644e2d81638842a95"), "sid" : 58, "sname" : "rusty", "rating" : 10, "age" : 35 }
{ "_id" : ObjectId("5d98671644e2d81638842a96"), "sid" : 64, "sname" : "horatio", "rating" : 7, "age" : 35 }
{ "_id" : ObjectId("5d98671644e2d81638842a97"), "sid" : 71, "sname" : "zorba", "rating" : 7, "age" : 16 }
{ "_id" : ObjectId("5d98671644e2d81638842a98"), "sid" : 74, "sname" : "horatio", "rating" : 9, "age" : 40 }
{ "_id" : ObjectId("5d98671644e2d81638842a99"), "sid" : 85, "sname" : "art", "rating" : 3, "age" : 25.5 }
{ "_id" : ObjectId("5d98671644e2d81638842a9a"), "sid" : 95, "sname" : "bob", "rating" : 3, "age" : 63.5,
"nickname" : [ "boo" ] }
{ "_id" : ObjectId("5d9868d644e2d81638842a9b"), "sid" : 22, "sname" : "Dustin", "rating" : 7, "age" : 29,
"nickname" : [ ] }
{ "_id" : ObjectId("5d9868d644e2d81638842a9c"), "sid" : 29, "sname" : "brutus", "rating" : 1, "age" : 33,
"nickname" : [ "bru", "bru" ] }
#update $pull
#pulls the values out from the coloumn
db.sailors.update({sid:22},{$pull:{nickname:'dust'}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.sailors.find();
{ "_id" : ObjectId("5d98671644e2d81638842a93"), "sid" : 31, "sname" : "lubber", "rating" : 8, "age" : 55 }
{ "_id" : ObjectId("5d98671644e2d81638842a94"), "sid" : 32, "sname" : "andy", "rating" : 8, "age" : 25.5 }
{ "_id" : ObjectId("5d98671644e2d81638842a95"), "sid" : 58, "sname" : "rusty", "rating" : 10, "age" : 35 }
{ "_id" : ObjectId("5d98671644e2d81638842a96"), "sid" : 64, "sname" : "horatio", "rating" : 7, "age" : 35 }
{ "_id" : ObjectId("5d98671644e2d81638842a97"), "sid" : 71, "sname" : "zorba", "rating" : 7, "age" : 16 }
{ "_id" : ObjectId("5d98671644e2d81638842a98"), "sid" : 74, "sname" : "horatio", "rating" : 9, "age" : 40 }
{ "_id" : ObjectId("5d98671644e2d81638842a99"), "sid" : 85, "sname" : "art", "rating" : 3, "age" : 25.5 }
{ "_id" : ObjectId("5d98671644e2d81638842a9a"), "sid" : 95, "sname" : "bob", "rating" : 3, "age" : 63.5,
"nickname" : [ "boo" ] }
{ "_id" : ObjectId("5d9868d644e2d81638842a9b"), "sid" : 22, "sname" : "Dustin", "rating" : 7, "age" : 29,
"nickname" : [ ] }
{ "_id" : ObjectId("5d9868d644e2d81638842a9c"), "sid" : 29, "sname" : "brutus", "rating" : 1, "age" : 33,
"nickname" : [ "bru", "bru" ] }
>
> db.sailors.find();
{ "_id" : ObjectId("5d98671644e2d81638842a93"), "sid" : 31, "sname" : "lubber", "rating" : 8, "age" : 55 }
{ "_id" : ObjectId("5d98671644e2d81638842a94"), "sid" : 32, "sname" : "andy", "rating" : 8, "age" : 25.5 }
{ "_id" : ObjectId("5d98671644e2d81638842a95"), "sid" : 58, "sname" : "rusty", "rating" : 10, "age" : 35 }
{ "_id" : ObjectId("5d98671644e2d81638842a96"), "sid" : 64, "sname" : "horatio", "rating" : 7, "age" : 35 }
{ "_id" : ObjectId("5d98671644e2d81638842a97"), "sid" : 71, "sname" : "zorba", "rating" : 7, "age" : 16 }
{ "_id" : ObjectId("5d98671644e2d81638842a98"), "sid" : 74, "sname" : "horatio", "rating" : 9, "age" : 40 }
{ "_id" : ObjectId("5d98671644e2d81638842a99"), "sid" : 85, "sname" : "art", "rating" : 3, "age" : 25.5 }
{ "_id" : ObjectId("5d98671644e2d81638842a9a"), "sid" : 95, "sname" : "bob", "rating" : 3, "age" : 63.5,
"nickname" : [ ] }
{ "_id" : ObjectId("5d9868d644e2d81638842a9b"), "sid" : 22, "sname" : "Dustin", "rating" : 7, "age" : 29,
"nickname" : [ ] }
{ "_id" : ObjectId("5d9868d644e2d81638842a9c"), "sid" : 29, "sname" : "brutus", "rating" : 1, "age" : 33,
"nickname" : [ "bru", "bru" ] }