MongoDB Flashcards

1
Q

How do you install MongoDB on windows?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does JSON stand for?

A

JavaScript Object Notation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s another name for a noSQL database? What are the benefits/when would you use it?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

After installing MongoDB, how do you start the shell?

A

You must download the shell, configure an environment variable on Path to point to the bin folder, and use command “mongosh”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you switch to a particular database within MongoDB from the command line? What if it doesn’t exist?

A

Type “use myDatabase” for example.

If that database doesn’t exist, the “use” command will create it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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.

A

db.createUser({
user: “Tom”,
pwd: “1234”,
roles: [“readWrite”, “dbAdmin”]
});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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?

A

db.users.find().pretty();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are some nuances to the insert function in MongoDB?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you update a document in a MongoDB collection?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you increment an existing field in a MongoDB document?

A

db.users.update({_id: “123”}, {$inc: {age:5}});

This increases the age value for the document with _id “123” by 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you remove a field called age in a MongoDB document?

A

db.users.update({_id: “123”}, {$unset: {age: 1}});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you rename a field in a document in MongoDB?

A

db.users.update({_id: “123”}, {$rename: {“gender”: “sex”}});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you remove a document from MongoDB shell?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

When making a query for a document with an field within an object, what must you do?

A

db.users.find({“address.city”:”Boston”});

If it’s a field within a field object, you have to wrap it in quotes like so. There are other situations when you need to use quotes as well, so keep that in mind.

17
Q

How do you query for documents in MongoDB that have the value mem1 within their membership array?

A

db.users.find({membership: “mem1”});

18
Q

How do you get all documents in a MongoDB collection and sort them by last name in descending order?

A

db.users.find().sort({last_name:1)}

Change 1 to -1 to make it ascending order.

19
Q

How do you get the amount of results that match a query?

A

db.users.find().count();

20
Q

How do you limit a MongoDB query to only include the first 4 results?

A

db.users.find().limit(4);

21
Q

How do you print the first names of each of the results returned from a MongoDB query?

A

db.users.find().forEach(function(doc){print(“User first name: “ + doc.first_name)});

22
Q

If you want to connect a NodeJS server to a MongoDB database over the cloud, what should you do?

A

Go to MongoDB Atlas and sign in. If you don’t already have a user, create one (database access tab) with read and write access that can authenticate through a username and password. Access should be given to all databases.

Go to database tab and click “connect” on appropriate database. Connect application to cluster using MongoDB’s native drivers. (Apparently UI changes frequently, but choose the one that connects your application).

Driver is Node.js. It should give example code.

(Unfinished)

23
Q

What is Mongoose?

A

Mongoose is an Object Document Mapping library. It’s what allows you to make Schemas and models, connect to a database, and use those variables to use the database.

24
Q

What is unique about connection to a local MongoDB database with NodeJS version 17 or later?

A

You can’t use localhost. You have to type 127.0.0.1, so:

mongoose.connect(‘mongodb://127.0.0.1:27017/portfolio’, {
useNewUrlParser: true
});