mongod
Note: open a new tab or instance of command prompt
mongo
show dbs
use <db_name>
When you create a value it will automatically create the collection name
db.staff.insert({"name": "Thomas"})
db.staff.insert({"name": "Donny"})
db.staff.insert({"name": "Clarence"})
db.students.insert({"name": "Mat"})
db.staff.insert({"name": "Danielle"})
db.staff.insert({"name": "Tracy"})
show collections
Find by any criteria:
db.students.find({"name": "Mat"})
db.staff.find()
db.students.find({ _id : ObjectId("5f0f59fb9331e12029ccf8f5")})
db.staff.update({"name": "Donny"}, {"name": "Don"})
db.staff.update({ _id : ObjectId("5f0f59fb9331e12029ccf8f5")}, {"name": "Donny"}
This is destructive. If there are any keys other user defined keys they will be erased and replaced with the name
key and value.
First create some examples in a new collection within the database.
db.projects.insert({"project": 1, "status": "complete"})
db.projects.insert({"project": 2, "status": "complete"})
db.projects.insert({"project": 3, "status": "not complete"})
db.projects.insert({"project": 4, "status": "fake"})
Note: The "projects" collection will be created automatically.
Update non destructively
db.projects.update({"project": 3}, { $set: {status: "incomplete"}})
Note: The $set operator will update the status key and keep all the previous keys and values (project)
Show all data in collection: db.projects.find()
db.projects.remove({"project" : 4})
db.projects.remove()
db.projects.drop({})
Note: An empty object needs to be passed. Cannot be left empty.
See which database you are using currently.
db
Drop database:
db.dropDatabase()