MongoDB Flashcards
How do you install MongoDB on windows?
Install from website
Run installation program and change where the items or stored if desired. Default is in program files.
Open a command line as admin, navigate to mongodb\bin folder, and run:
mongod —directoryperdb —dbpath C:\mongodb\data\db —logpath C:\mongodb\log\mongo.log —logappend —rest —install
Then type this to run it as a service (in the background):
net start MongoDB
What does JSON stand for?
JavaScript Object Notation
What’s another name for a noSQL database? What are the benefits/when would you use it?
Also called document database.
Easier to scale and typically faster operations
Use when there aren’t interconnected relationships between objects being uploaded. Ton of objects.
After installing MongoDB, how do you start the shell?
You must download the shell, configure an environment variable on Path to point to the bin folder, and use command “mongosh”
How do you switch to a particular database within MongoDB from the command line? What if it doesn’t exist?
Type “use myDatabase” for example.
If that database doesn’t exist, the “use” command will create it.
How do you create a user for a particular database in MongoDB from the MongoDB shell? Include info to give them read-write privileges and make them an admin for the database.
db.createUser({
user: “Tom”,
pwd: “1234”,
roles: [“readWrite”, “dbAdmin”]
});
What is a collection within a MongoDB database? How do you make one, see one, and insert an item (document) into the collection? What is automatically generated when adding a document?
A collection is equivalent to a table in a relational database. If we have a bunch of user we want to store, we could make a Users collection.
db.createCollection(‘users’);
To see all collections in the database:
show collections
To insert an item into the collection:
db.users.insert({ first_name: “John”, last_name: “Doe”})
When inserting an item, the “_id” field is automatically generated to distinguish items. It’s basically a unique hash to identify that document.
How do you query for all items in a MongoDB collection “users” when using the appropriate database in the shell? And make it look neat?
db.users.find().pretty();
What are some nuances to the insert function in MongoDB?
You can add multiple JSON objects in a single insert by putting an array of objects (separated by a comma) between the parentheses in the insert function.
You can also add an item to the collection with an additional field. It doesn’t have to match a particular schema (at least when doing it from the shell).
You can also insert an item using the update command if you add a third parameter called “upsert”:
db.users.update({_id: “123”}, {name: “John”, gender: “male”}, {upsert: true});
How do you update a document in a MongoDB collection?
db.users.update({_id: “123”}, {first_name: “John”, last_name: “Doe”, gender: “male”});
When updating, you must include all the fields that are correct as well or else the whole document will only have the fields you changed. Make it exactly what you want it to be.
If you don’t want to deal with that, you can use $set like this:
db.users.update({_id: “123”}, {$set: {gender: “male”}});
This only adds/changes the gender field and keeps the rest as is.
How do you increment an existing field in a MongoDB document?
db.users.update({_id: “123”}, {$inc: {age:5}});
This increases the age value for the document with _id “123” by 5
How do you remove a field called age in a MongoDB document?
db.users.update({_id: “123”}, {$unset: {age: 1}});
How do you rename a field in a document in MongoDB?
db.users.update({_id: “123”}, {$rename: {“gender”: “sex”}});
How do you remove a document from MongoDB shell?
db.users.remove({_id: “123”});
This second way only removes the first instance if you were using a duplicate field like first name instead of a unique id:
db.users.remove({_id: “123”}, {justOne: true});
How do you find all documents that have either first name Sharon or first name Tom in a MongoDB collection? All that are under the age of 40?
db.users.find({$or:[{first_name:”Sharon”},{first_name:”Tom”}]});
The query takes one argument ($or) that points to an array of document fields that specific which criteria to pull from.
db.users.find({age:{$lt:40}).pretty();
gt: greater than
lte: less than or equal to
gte: greater than or equal to