show databases
OR show dbs
show collections
use database_name
db.createCollection('collection_name')
you can define variable (js syntax) and pass it as excepted parameter.
var user = {
name: "Mohammad MohammadAlian",
ip: "127.0.0.1",
lastLoginTime: 1575704736
};
db.insertOne(user);
OR
db.collection.insertOne({name: 'Mohammad MohammadAlian', ip: '127.0.0.1', lastLoginTime: 1575704736})
;
to insert many object in one query you can use insertMany function
var users = [
{
name: "Mohammad MohammadAlian",
ip: "127.0.0.1",
lastLoginTime: 1575704736
},
{ name: "John Doe", ip: "10.10.10.10", lastLoginTime: 1575704965 }
];
db.insertMany(users);
OR
db.collection.insertMany([{name: 'Mohammad MohammadAlian', ip: '127.0.0.1', lastLoginTime: 1575704736}, {name: 'John Doe', ip: '10.10.10.10', lastLoginTime: 1575704965}])
;
retrieve all of documents
db.collection.find()
OR db.collection.find({})
query by value of specific field
db.collection.find({name: 'Mohammad MohammadAlian'})
querying throw nested field
var user = { name: "John", job: { title: "programmer", salary: 125000 } };
if we want to find above user by job title we could use following command
db.collection.find({'job.title': 'programmer'})
using regex
db.collection.find({name: /M.*/})
make results pretty 😁
db.collection.find().pretty()
db.collection.find().limit(10)
NOTE: 10 is count of documents which will be retrieved
skip result
db.collection.find().skip(2)
NOTE: 2 is count of documents which will be skipped
show sorted result
db.collection.find().sort({fieldName: 1})
NOTE: 1 is ascending and -1 is descending
retrieve count of results
db.collection.find().count()
retrieve distinct value
db.collection.distinct('name')
NOTE: name is a field
for nested field we can use following command
db.collection.distinct('comments.message')
db.collection.find({field: {$lt: 200}})
db.collection.find({field: {$lte: 200}})
db.collection.find({field: {$gt: 200}})
db.collection.find({field: {$gte: 200}})
db.collection.find({field: {$ne: 'string is also accepted in some operators'}})
db.collection.find({field: {$in: [1999,2010,2019,2022]}})
db.collection.find({field: {$all: [1999,2010]}})
db.collection.find({arrayField: {$slice: 3}})
db.collection.find({$or: [{filed: 'value'}, {field: 'value'}]})
modulo operator
db.collection.find({field: {$mod: [100,0]}})
db.collection.find({arrayFiled: {$size: 2}})
db.collection.find({field: {$exists: true}})
type numbers
db.collection.find({field: {$type: 2}})
general form
db.collection.update({query}, {update}, {flags})
example
db.collection.update({field: 'value'}, {$set: {otherField: 'new Value'}}, {upsert: true})
increment value
db.collection.updateOne({field: 'value'}, {$inc: {number: 6}})
Increases six units of number
db.collection.updateOne({field: 'value'}, {$unset: {anotherField: 1}})
this will be remove anotherField where field equal value
push value to array
db.collection.updateOne({_id: 1}, {$push: {numbers: 6}})
push six into numbers where id is 1
db.collection.updateOne({_id: 1}, {$push:{numbersArray: {$each: ['R','T', 'H']}}})
add each value to numbersArray where id is 1
push to array if not exists
db.collection.updateOne({_id:1}, {$addToSet: {numbers: 565}})
db.collection.updateOne({_id: 1}, {$pop: {numbers: -1}})
1: from end
-1: from beginning
db.collection.updateOne({_id: 1}, {$pull: {numbers: 5}})
remove all 5 in numbers Array where id is 1
db.collection.updateOne({_id: 1}, {$pullAll: {numbers: [4,5,6]}})
db.collection.renameCollection('newCollectionName')
db.collection.deleteOne({_id: 1})
db.collection.drop()
db.dropDatabase()
db.collection.createIndex({field: 1})
NOTE: 1 is ascending and -1 is descending
create text index
db.collection.createIndex({field: 'text'})
db.collection.dropIndex({field: 1})
db.collection.aggregate({ $group: {_id: '$color'} })
db.collection.aggregate({ $group: {_id: '$color', count: {$sum: 1}} })
db.aggregation.aggregate({ $group: {_id: "$color"} });
db.aggregation.aggregate([ {$match: {num: {$gt: 500}}}, {$group: {_id: '$color', count: {$sum: 1}}}, {$limit: 5} ]);
db.aggregation.aggregate([ {$match: {num: {$gt: 500}}}, {$sort: {color: 1}} ]);
db.aggregation.aggregate({ $unwind: "$vegetables" });
db.aggregation.aggregate([ {$match: {num: {$gt: 500}}}, {$project: {_id:0, fruits: 0}}, {$skip: 200}, {$out: "newCollectionGeneratedByAggregationFramework"} ]);
db.prima.aggregate({ \$lookup: { from: 'secunda', localField: 'number', foreignField: 'number', as: 'doc' } });