- Download or do brew
- It's kinda JSONy
- You can launch in the background or foreground:
Foreground:
mongod --config /usr/local/etc/mongod.conf
Background:
brew services start mongodb
- Then launch it using
mongo
What you can do:
show dbs
use DBNAME
to create dbdb
to whereamidb.createUser({})
- So let's create user
db.createUser({
user: "NAME",
pwd: "PASS",
role: ["readWrite" , "dbAdmin"]
});
- and create collection...
db.createCollection("COLLECTIONNAME")
is like a tabledb.COLLECTION.insert({})
db.COLLECTION.find()
and it is scalable!
For example:
db.customers.insert([{first_name: "Regina", last_name: "Valetova"},{first_name: "Ivan", last_name: "Dorow", age: 13}])
Or to make prettier: db.COLLECTION.find().pretty();
- In order to Match and update:
For example:
db.customers.update({first_name: "Regina"},{first_name: "Regina", last_name: "Waletou", age: 14})
However, if we want to change just one cell, we can do:
db.customers.update({first_name:"Regina"},{$set:{hasSpouse: true}})
db.customers.update({last_name: "Valetova"},{$inc:{age:1}})
Deleting an option:
db.customers.update({first_name:"Regina"},{$unset:{hasSpouse: true}})
If we want to match something and edit, but it isn't there, it won't make anything, yet we can create new object, in case none was found:
db.customers.update({first_name:"Regina"},{$set:{hasSpouse: true},{upsert: true}})
if we want to rename the key:
db.customers.update({first_name: "Brad"},{$rename: {"age": "old"}})
db.customers.remove({first_name: "Darth"})
db.customers.find({$or:[{first_name: "Luke"},{age: 22}]})
db.customers.find({age:{$gt:14}})
`db.customers.find(["adress.city": "Boston"]);
db.cutomers.find().sort({last_name: 1});
Would sort alphabetically by last name, or -1
if reversed
db.customers.find().count()
db.customers.find().limit(4)
db.customers.find().forEach(function(doc){print("Customer Name: " + doc.first_name)});