show dbs
: shows you the name of your databases. This will only show output, when the db will have atleast one collectionuse <db name>
: creates DB or switch DBuse college
db.dropDatabase()
: deletes the particular databasedb.createCollection('students')
: creates students collection in college DB, heredb.
meansstudents.
db.<collection name>.insertOne({})
: inserting one row (document)db.students.insertOne({name : "vikash", age : 21, profession : "btech"})
: inserting one row (document) in students collection.
db.<collection name>.insertMany([ {}, {}, {} ])
: insert more than one row at once.db.students.insertMany([ {name : "ashish", age : 43, profession : "finance"}, {name : "suman", age : 26, profession: "House wife"} ])
db.books.insertOne({
title: "The Way of Kings", author: "Brandon Sanderson", rating: 9, pages: 400, genres: ["fantasy", "adventure"], reviews: [
{name: "Yoshi", body: "Great book!!"},
{name: "mario", body: "so so"}
]
})
- Ordered Insertion video
- Ordered insertion means that MongoDB processes each document one by one.
- If one document fails to insert due to an error (like a duplicate ID), MongoDB stops inserting further documents but keeps the ones that succeeded.
- if there's a duplicate ID, MongoDB throws an error and stops inserting further documents.
- we can change this behavior by setting "ordered" to false, allowing MongoDB to continue inserting documents after encountering an error.
db.<collection name>.insertMany([ {}, {}, {} ], {ordered : false})
- Even if some documents fail to insert, the ones that succeeded remain in the collection.
- They emphasize that once a document is successfully inserted, it won't roll back even if later documents encounter errors.
db.students.find().pretty()
: shows all the row of students collection in prettified format.db.students.find({name : "ashish", age : 43}).pretty()
: find documents instudents
collection whose name : "ashish" and age : "43"db.students.find({age : {$gte : 50}}).count()
: returns the count.db.students.find({age : {$gte : 50}}).limit(3)
: this limits the number of document we get for particular filtered query.db.students.find({name : "ashish", age : {$lt : 50}}).pretty()
db.students.find({name : "ashish", age : {$lte : 50}}).pretty()
db.students.find({name : "ashish", age : {$gt : 50}}).pretty()
db.students.find({name : "ashish", age : {$gte : 50}}).pretty()
db.students.find({age : {$in : [12, 300, 9, 200]}})
: show all document which has age includes 12, 300, 9, 200db.students.find({age : {$nin : [12, 300, 9, 200]}})
: show all document which has age not includes 12, 300, 9, 200db.products.find({price: {$exists: true}}).count()
: This will give us the documents having/not-having particular field.db.books.find({reviews : {$size : 2}})
: this will give me the books having 2 reviewsdb.students.find({math_score : {$type : 'int'}}).count()
: This will give us the documents count having field of particular type. BSON types doc
Shows document fields of your choice [either give what to-show orelse what not-to-show, dont give both (_id is exception)]
db.books.find({search query}, {fields to show / fields not-to show})
db.books.find({ 'reviews': {$size:2}}, {title:1, author:1}
: shows title and author onlydb.books.find({ 'reviews': {$size:2}}, {title:0, author:0}
: shows other-field except title and author.
{
_id: ObjectId("622b746cd28eddc86c808d41"),
title: '1984',
author: 'George Orwell',
pages: 300,
rating: 7,
genres: [ 'sci-fi', 'dystopian', 'fantasy' ],
reviews: [
{ name: 'luigi', body: 'not my cup of tea' },
{ name: 'mario', body: 'meh' }
],
metadata: { views: 1500, likes: 60 }
}
db.books.find().sort({ rating: 1 })
: sort ascending order of ratingdb.books.find().sort({ rating: -1 })
: sort descending order of ratingdb.books.find({$or: [{rating: 7}, {rating: 9}]})
: we give array of filters, if anyone of the filters matches it print that document.db.books.find({$or: [{pages: {$lt: 300 }}, {pages: {$gt: 400}}]})
db.books.find({$and: [{pages: {$gt: 500}}, {pages: {$lt: 1300 }}]})
: both condition should be true.db.books.find({genres: {$all: [fantasy", "sci-fi"]}})
- we are finding books which has genres[] containing both(fantasy and sci-fi) atleast.
db.books.find({genres: "fantasy"})
: this will find book which has genres as fantasy atleast.db.books.find({"reviews.name": "luigi"})
db.books.find({"metadata.views": {$gt : 1200}})
db.books.find({"reviews.name": "luigi", "metadata.views": {$gt : 1200}})
db.books.find({'reviews.name': {$all: ['luigi', 'mario'] }
Itteration of document
db.<collection_name>.find().forEach(function(doc) {
print("Key: " + doc.<key> + " Value: " + doc.<value>);
})
db.books.updateOne({_id: ObjectId("622b746cd28eddc86c808d40")}, {$set: {rating: 8, pages: 360}})
db.books.updateMany({author: "Terry Pratchett"}, {$set: {author: "Terry Pratchet"}})
db.books.updateOne({_id: ObjectId("622b746cd28eddc86c808d41")}, {$inc: {pages: 2}})
: increment the pages by 2.db.books.updateOne({_id: ObjectId("622b746cd28eddc86c808d41")}, {$inc: {pages: -2}})
: decrement the pages by 2.db.books.updateOne({_id: ObjectId("622b746cd28eddc86c808d41")}, {$pull: {genres: "fantasy"}})
: this will remove the "fantasy" from genres[]db.books.updateOne({_id: ObjectId("622b746cd28eddc86c808d41")}, {$push: {genres: "fantasy"}})
: this will add the "fantasy" in genres[]db.books.updateOne({_id: ObjectId("622b746cd28eddc86c808d41")}{$push:{genres: {$each: ["1","2"]}}})
: this will add more than one value in genres[]
db.students.deleteMany({id : {$gt:2}})
db.students.deleteMany({})
: delete all documents in students collection
mongoimport <filePath\filename.json> -d <db name> -c <collection name>
{}, {}, {}mongoimport products.json -d shop -c products --jsonArray
if .json is [{} {} {}]