Repository files navigation
db.createCollection('collection_name')
db.collection_name.drop()
INSERTING SINGLE DATA IN DOCUMENT
db.document_name.insertOne({object})
INSERTING MULTIPLE DATA IN DOCUMENT
db.document_name.insertMany([{object1}, {object2}])
📍📍 ORDERED & UNORDERED INSERTS: determines the batch behaviour
By default : ordered (MongoDB stops after encountering an error)
unordered (continues to perform the write operations): db.col_name([],{ordered: false})
count()
limit()
skip()
sort()
IMPORTING AND EXPORTING JSON FILES
in root directory: mongoimport json_file_path -d db_name -c collection_name
in root directory: mongoexport -d db_name -c collection_name -o json_file_path
USING EXPRESSIONS IN COMMANDS
db.col_name.find({$expr: {$gt: [{$multiply: ['$field1','$field2']},'field3']}})
db.document_name.find({property_name: value})
db.document_name.sort({property_name: 1}) //sorts in ascending order
db.document_name.sort({property_name: -1}) //sorts in descending order
OPERATORS & COMPLEX QUERIES
Using $gte, $lte, $or : db.shop.find({$or: [{price: {$lte: 100}}, {price: {$gte: 100}}]})
Using $in, $nin : db.shop.find({product: {$in: ["notebook","crayons"]})
using $set : db.doc_name.updateOne({_id: object_id}, {$set: {key: value}})
using $inc : db.doc_name.updateOne({_id: object_id}, {$inc: {key: increment_val}})
using $pull & $push: db.doc_name.updateOne({_id: object_id}, {$pull: {key: value}}) //operates ony on arrays
REMOVING AND RENAMING FIELDS IN DOCUMENTS
db.col_name.updateOne({filter}, {$unset: {fieldName: 1}})
db.col_name.updateOne({filter}, {$rename: {oldFieldName: newFieldName}})
db.col_name.deleteOne({filter})
db.col_name.deleteMany({filter})
db.col_name.createIndex({fieldName: 1})
db.col_name.getIndexes()
db.col_name.dropIndex({fieldName: 1})
db.col_name.aggregate({$match: {query}})
db.col_name.aggregate([{$group: {_id: '$fieldName'}, varname: {$sum: 1}}])
db.col_name.aggregate([{$project: {fieldName: 1, 'varName': {$filter: {input: '$fieldName', as: 'alias', cond: {$gt: ['$$alias', value]}}}}}}])
About
This repository provides a comprehensive guide to MongoDB, an open-source, document-oriented NoSQL database. It includes MongoShell commands ranging from basic to advanced levels and showcases projects integrating Node.js and MongoDB.
Topics
Resources
Stars
Watchers
Forks
You can’t perform that action at this time.