show dbs
use rohits05 (database name)
db (gives the current database)
db.dropDatabase()
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----
db.createCollection("rohits05") (collection name)
show collections
db.createCollection('comics')
db.comics.insertOne({name: "Harry Potter", author: "J.K. Rowling"})
db.comics.insertMany([{name: "Harry Potter", author: "J.K. Rowling"}, {name: "Harry Potter", author: "J.K. Rowling"}])
db.comics.drop()
db.comics.deleteOne({name: "Harry Potter"})
db.comics.deleteMany({name: "Harry Potter"})
db.comics.deleteMany({}) db.comics.remove({name: "Harry Potter"}) (remove() is deprecated)
db.comics.find()
db.comics.find().pretty()
db.comics.find().pretty().limit(2)
db.comics.find().pretty().limit(2).skip(1)
db.comics.find().pretty().limit(2).skip(1).sort({name: 1})
db.comics.find().pretty().limit(2).skip(1).sort({name: 1}).count() ( 1: ascend_sort, -1: descend_sort )
🌿To Search all data in collection in a better way with limit and skip and sort and count and find specific data
db.comics.find({name: "Harry Potter"}).pretty().limit(2).skip(1).sort({name: 1}).count()
db.comics.findOne({name: "Harry Potter"})
db.comics.findOne({name: "Harry Potter"}).pretty()
db.comics.findOne({name: "Harry Potter"}).pretty().limit(1)
db.comics.findOne({name: "Harry Potter"}).pretty().limit(1).skip(1)
db.comics.findOne({name: "Harry Potter"}).pretty().limit(1).skip(1).sort({name: 1})
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----
db.comics.updateOne({name: "Harry Potter"}, {$set: {name: "Harry Potter and the Philosopher's Stone"}}) (updateOne or update)
db.comics.updateMany({name: "Harry Potter"}, {$set: {name: "Harry Potter and the Philosopher's Stone"}})
db.comics.updateOne({name: "Harry Potter"}, {$set: {name: "Harry Potter and the Philosopher's Stone"}}, {upsert: true}) (upsert: true means if the data is not present then it will create a new data)
db.comics.updateMany({name: "Harry Potter"}, {$set: {name: "Harry Potter and the Philosopher's Stone"}}, {upsert: true})
db.comics.updateOne({name: "Harry Potter"}, {$set: {name: "Harry Potter and the Philosopher's Stone"}}, {upsert: true, returnOriginal: false})
db.comics.updateMany({name: "Harry Potter"}, {$set: {name: "Harry Potter and the Philosopher's Stone"}}, {upsert: true, returnOriginal: false})
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----
db.comics.updateOne({name: "Harry Potter"}, {$rename: {"name": "book_name"}})
db.comics.updateMany({name: "Harry Potter"}, {$rename: {"name": "book_name"}})
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----
db.comics.updateOne({name: "Harry Potter"}, {$inc: {"price": 100}})
db.comics.updateMany({name: "Harry Potter"}, {$inc: {"price": 100}})
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----
db.comics.updateOne({name: "Harry Potter"}, {$unset: {"price": ""}})
db.comics.updateMany({name: "Harry Potter"}, {$unset: {"price": ""}})
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----
db.comics.find({price: {$lt: 100}}).pretty()
db.comics.find({price: {$lte: 100}}).pretty()
db.comics.find({price: {$gt: 100}}).pretty()
db.comics.find({price: {$gte: 100}}).pretty()
db.comics.find({price: {$ne: 100}}).pretty()
db.comics.find({price: {$gt: 100, $lt: 200}}).pretty()
db.comics.find({price: {$gte: 100, $lte: 200}}).pretty()
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----