JavaScript Set 8 Flashcards

1
Q

What are the naming differences between a RDBMS and MongoDB?

A
  1. Table is called a collection
  2. A row is called a document
  3. A column is called a field
  4. A JOIN is called linking and embedding
  5. A foreign key is called a reference
  6. A partition is called a shard
  7. A primary key is _id
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are CRUD operations?

A

Create, Read, Update, and Delete

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

How is a MongoDB database created?

A

There is no create statement
1. Switch context to a non-existing database
use db_name
2. Mongo will only create the database when data is first stored
db.colelction.insert({document data});

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

How can you retrieve documents from a MongoDB database?

A

db.collection.find({query criteria}. {projection}).limit(number)
Ex:
db.users.find(
{ age” {$gt: 18} },
{ name: 1, address: 1 }
).limit(5)
The projection allows you to explicitly include (1) or exclude (0) fields

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

How can you install MongoDB to use with node.js?

A

Install with npm
npm install mongodb

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

What happens if you try to install MongoDB and MySQL to the same folder with node.js

A

You will get an error. You have to separate the drivers

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

How do you connect to MongoDB from node.js?

A

var mongoClient = require(‘mongodb’).MongoClient;
var url = “mongodb://localhost:port/dbname”;

mongoClient.connect(url, function(err, db) { });

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

What is the general form of a URL for connecting to MongoDB?

A

mongodb://user:pass@sample.host:port/connectionoptions

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

What is the syntax for creating a collection in MongoDB with node.js?

A

MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db(“mydb”);
dbo.createCollection(“persons”,
function(err, res) {
if (err) throw err;
console.log(“Collection ‘persons’
created!”);
db.close();
});
});

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

What is the syntax for inserting a document to a collection in MongoDB with node.js?

A

MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db(“mydb”);
var myobj = { lastName: “Hao”,
firstName: “Yuan”, age: 28 };
dbo.collection(“persons”).insertOne(myobj, function(err, res) {
if (err) throw err;
console.log(“1 document inserted”);
db.close();
});
});

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

What is the syntax for updating a document in MongoDB with node.js?

A

MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db(“mydb”);
var myquery = { age: 28 };
var newvalues = { $set: {lastName: “Mao”, firstName: “Zheng” } };
dbo.collection(“persons”).updateOne( myquery, newvalues, function(err, res)
{
if (err) throw err;
console.log(“1 document updated”);
db.close();
});
});

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

What is the difference between findOne() and find() in MongoDB?

A

findOne returns the first occurrence in the selection whereas find returns all occurences in the selection

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

What is the syntax for finding a single document in mongoDB?

A

MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db(“mydb”);
dbo.collection(“persons”).findOne({condition}, function(err, result) {
if (err) throw err;
console.log(result.lastName);
db.close();
});
});

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

What is the syntax for finding many documents in mongoDB?

A

MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db(“mydb”);
dbo.collection(“persons”).find({condition}).toArray(function(err, result) {
if (err) throw err;
console.log(result);
db.close();
});
});

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

What is the syntax for deleting documents in mongoDB?

A

MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db(“mydb”);
var myquery = { age: 48 };
dbo.collection(“persons”).deleteMany(myquery, function(err, obj) {
if (err) throw err;
console.log(obj. deletedCount + “
document(s) deleted”);
db.close();
});
});

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

What is the difference between SQL and noSQL?

A
  1. SQL is a relational database, NoSQL is a non-relational database
  2. SQL has a pre-defined schema, NoSQL has a dynamic schema
  3. SQL has table based databases, NoSQL has document-based databases, key-value stores, graph stores,wide column stores
  4. SQL is vertically scalable, NoSQL is horizontally scalable
  5. SQL uses structured query language, NoSQL uses unstructured query language