w5 - mongoDB Flashcards

1
Q

mongoDB

A

free document database management system that offers high scalability, availability, and flexibility

JSON-like documents, which means fields can vary from document to document, and data structure can be changed over time

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

Document DB?

A

A record in MongoDB is a document, which is a data structure composed of field and value pairs. MongoDB documents are similar to JSON objects. The values of fields may include other documents, arrays, and arrays of documents.

composed of field and value pairs

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

difference between mongoDB and SQL?

A

SQL MongoDB
Database Database
Table Collection
Index Index
Row Document
Column Field

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

Diff b/w mongoDB and JSON?

A

Compared to a JSON object, a MongoDB document has support for the primitive data types boolean, numbers, and strings and other common data types such as dates, timestamps, regular expressions, and binary data.

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

MongoDB id?

A

A primary key is mandated in MongoDB and has the reserved field name _id.

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

Benefits of compass MongoDB?

A

Visually explore your data.

Run ad hoc queries in seconds.

Interact with your data with full CRUD functionality

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

MongoClient.connect(url, {useNewUrlParser: true}, function (err, client) {
if (err) {
console.log(‘Err ‘, err);
} else {
console.log(“Connected successfully to server”);
db = client.db(‘fi2095table’);
}
});

A

The first parameter is the server’s URL, which is a local server listening at port 27017 (MongoDB default port)

The second parameter is an object that is required for the latest version of MongoDB (version >4/0)

The third parameter is a callback function that will get executed after the connect operation finishes. It has two parameters: err object that gets value if an error occurs and client which is used later to access the database as shown in line 6 that connects to a database named ‘fit2095db

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

Create

db.collection(‘week5table’).insertOne({name: ‘Tim’});

db.collection(‘week5table’).insertMany([
{ name: ‘Alex’, age: 25 },
{ name: ‘John’, age: 34 },
{ name: ‘Max’, age: 26 }
]);

A

The above statement references a collection named ‘week5table’. The statement creates the collection if it does not exist. Now, let’s insert a document that has one property {name:’Tim’}

insert multiple docs

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

Read

findOne

find()

A

findOne-
returns one document that satisfies the specified query criteria on the collection. If multiple documents satisfy the query, this method returns the first document according to the natural order, which reflects the order of documents on the disk.

db.collection(“week5table”).findOne({ name: “Tim” }, function (err, result) {
console.log(result);
});

The find() method returns all occurrences in the selection.

db.collection(“week5table”).find({}).toArray(function (err, result) {
console.log(result);
});

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

QUERY

A

To filter the result of the find() method, a query (i.e. criteria)object should be used.
let query = { name: ‘Alex’ };
db.collection(“week5table”).find(query).toArray(function (err, result) {
if (err) throw err;
console.log(result);
});

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

get all the documents where the name starts with ‘T’?

A

let query = { name: /^T/ };
db.collection(“week5table”).find(query).toArray(function (err, result) {
if (err) throw err;
console.log(result);
});

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

Finds all the documents where the name ends with ‘x’:

A

let query = { name: /x$/ };
db.collection(“week5table”).find(query).toArray(function (err, result) {
if (err) throw err;
console.log(result);
});

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

$gte
$lte

A

let query = { age: { $gte: 25 } };
db.collection(“week5table”).find(query).toArray(function (err, result) {
if (err) throw err;
console.log(result);
});

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

The list of Operators:

A

$currentDate Sets the value of a field to current date, either as a Date or a Timestamp.
$inc Increments the value of the field by the specified amount.
$min Only updates the field if the specified value is less than the existing field value.
$max Only updates the field if the specified value is greater than the existing field value.
$mul Multiplies the value of the field by the specified amount.
$rename Renames a field.
$set Sets the value of a field in a document.
$setOnInsert Sets the value of a field if an update results in an insert of a document. Has no effect on update operations that modify existing documents.
$unset Removes the specified field from a document.

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

Sorting result?

A

The sort() method can be used to sort the result in ascending or descending order. The sort parameter contains field and value pairs in the following form:
{ field: value }

1-> ascending
-1->descending

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

Sorting e.g?

A

let query = { age: { $gte: 25 } };
let sortBy={age:-1,name:1}
db.collection(“week5table”).find(query).sort(sortBy).toArray(function (err, result) {
if (err) throw err;
console.log(result);
});

17
Q

Limit results?

A

We use the limit() method to limit the result in MongoDB. Example: return the top 5 documents that have age>=25.
let query = { age: { $gte: 25 } };
let sortBy = { age: -1 }
db.collection(“week5table”).find(query).sort(sortBy).limit(5).toArray(function (err, result) {
if (err) throw err;
console.log(result);
});

18
Q

DELETE

deleteOne

deleteMany

A

db.collection(“week5table”).deleteOne({ name: ‘Tim’ }, function (err, obj) {
console.log(obj.result);
});

db.collection(“week5table”).deleteMany({age: { $gte: 25 }}, function (err, obj) {
console.log(obj.result);
});

19
Q

UPDATE-

collection.updateOne()

A

collection.updateOne() updates a single document based on the filter.

20
Q

upset

A

option: a set of options to the update method such as upset that creates a new document if no documents match the filter. Example: update the age of a document with name=’Tim’ to be 31.
db.collection(“week5table”).updateOne({ name: ‘Tim’ }, { $set: {age: 31 } }, { upset: true }, function (err, result) {
});

db.collection(“week5table”).updateMany({ name: /x$/ }, { $inc: { age: 2 } }, { upsert: true }, function (err, result) {
});

21
Q

High availability?

A

MongoDB high availability is achieved by the replication facility, which is called replica set