JavaScript Set 8 Flashcards
What are the naming differences between a RDBMS and MongoDB?
- Table is called a collection
- A row is called a document
- A column is called a field
- A JOIN is called linking and embedding
- A foreign key is called a reference
- A partition is called a shard
- A primary key is _id
What are CRUD operations?
Create, Read, Update, and Delete
How is a MongoDB database created?
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 can you retrieve documents from a MongoDB database?
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 can you install MongoDB to use with node.js?
Install with npm
npm install mongodb
What happens if you try to install MongoDB and MySQL to the same folder with node.js
You will get an error. You have to separate the drivers
How do you connect to MongoDB from node.js?
var mongoClient = require(‘mongodb’).MongoClient;
var url = “mongodb://localhost:port/dbname”;
mongoClient.connect(url, function(err, db) { });
What is the general form of a URL for connecting to MongoDB?
mongodb://user:pass@sample.host:port/connectionoptions
What is the syntax for creating a collection in MongoDB with node.js?
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();
});
});
What is the syntax for inserting a document to a collection in MongoDB with node.js?
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();
});
});
What is the syntax for updating a document in MongoDB with node.js?
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();
});
});
What is the difference between findOne() and find() in MongoDB?
findOne returns the first occurrence in the selection whereas find returns all occurences in the selection
What is the syntax for finding a single document in mongoDB?
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();
});
});
What is the syntax for finding many documents in mongoDB?
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();
});
});
What is the syntax for deleting documents in mongoDB?
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();
});
});