- use collection_name
- db.createCollection('collection_name')
- db.collection_name.drop()
- db.dropDatabase()
- db.document_name.insertOne({object})
- db.document_name.insertMany([{object1}, {object2}])
- 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()
- $exists
- $type
- 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
- 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
- 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
- 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]}}}}}}])