Simple commands you should know!
db.createUser({
user: "admin",
pwd: "admin",
roles: ["readWrite","dbAdmin"]
});
db.createCollection('customers');
show collections
db.customers.insert({first_name: "Ivan", last_name: "Ivanov"});
db.customers.find();
db.customers.insert([
{first_name: "Ivan1", last_name: "Ivanov1"},
{first_name: "Ivan2", last_name: "Ivanov2"},
{first_name: "Ivan3", last_name: "Ivanov3", gender:"male"},
]);
db.customers.find().pretty();
//Update only the first match for the key and replace all the values (if not set will remove the old one)
db.customers.update({first_name: "Ivan2"}, {last_name: "222"});
//$set will keep the previous values and add the new one
db.customers.update({first_name: "Ivan2"}, {$set:{age: 45}});
//will increment with 5 the age value
db.customers.update({first_name: "Ivan2"}, {$inc:{age: 5}});
db.customers.update({first_name: "Ivan2"}, {$unset:{age: 1}});
//if not found, insert it
db.customers.update(
{first_name: "Mariyka"},
{first_name: "Mariyka", last_name: "Mariykova", gender:"female"},
{upsert: true}
);
//rename field
db.customers.update({first_name: "Ivan2"}, {$rename:{"gender":"sex"}});
db.customers.remove({last_name: "222"});
db.customers.remove({last_name: "222"},{justOne:true});
db.customers.find({first_name: "Ivan"});
db.customers.find({$or:[{first_name: "Ivan"},{first_name: "Mariyka"}]});
//greater than ($gt)
db.customers.find({age:{$gt:40}}).pretty();
db.customers.find({"address.city":"Varna"}).pretty();
db.customers.find().sort({last_name:1}).pretty();
db.customers.find({gender:"male"}).count();
db.customers.find().limit(2);
db.customers.find().forEach(function(doc){print("Customer name: "+doc.first_name)});