MongoDB Flashcards
What kind of database is mongoDB?
NoSQL
What is NoSQL
Not only stuctured query langauge
Difference between SQL and noSQL?
SQL stores data in a table. An entry is called a row/record. Individual items in a row are called a column
noSQL stores data in a collection (looks like JSON). An entry is called a document. Individual items in a document are called fields
Installing community edition on machine
Download
Rename folder and move to User directory
Create another folder with same name + -data
in terminal run: mongod.exe –dbpath=/Users/DRPC/mongodb-data
How to start database?
In the terminal from the mongodb folder run:
mongod.exe –dbpath=/Users/DRPC/mongodb-data
What is robo3t?
a GUI tool for working with mongodb
What language does the mongo shell use?
javascript
How to check if connection to data base was successful using the mongo shell?
run in console:
db.version()
What do we need to install in order to interact with a mongoDB database from node?
mongoDb driver
npm i mongodb
How to connect to mongoDB using node?
const mongodb = require(‘mongodb’);
const MongoClient = mongodb.MongoClient;
const connectionURL = 'mongodb://127.0.0.1:27017'; const databaseName = 'task-manager';
MongoClient.connect(connectionURL,{useNewUrlParser:true,useUnifiedTopology: true },(error,client)=>{
if(error){
return console.log(‘Unable to connect to database’)
}
const db = client.db(databaseName)
});
What is the structure of a mongoDB database?
MongoDB contains databases
Databases contain collections
Collections contain documents
Documents contain fields
how to insert a document into a collection?
db.collection(‘users’).insertOne({
name:’Andrew’,
age:27
},(error,result)=>{
if(error){
return console.log(‘Unable to insert user’)
}
console.log(result.ops) })
How to create a MongoDb client?
const mongodb = require(‘mongodb’)
const MongoClient = mongodb.MongoClient
MongoClient(url,options,callback)
How to create a database?
const db = client.db(‘database-name’)
How to access or creat a collection within a database?
db.collection(‘collection-name’)
What methods can be used to insert documents into a collection
.insertOne(document,callback)
.insertMany(ArrayOfDocuments,callback)
What are gu id’s
globally unique identifiers. Allows mongo db to scale and handle a lot of traffic, without conflict.
What is an Object id?
It is a globally unique 12 byte identifier for a document.
Each 12 byte id constist out of a 4 byte time stamp, a 5 byte random value, and a 3 byte counter starting with a random number.
Are object id’s strings?
No they are 12 byte binary data
What methods can be used to retrieve documents from a collection?
.find()
.findOne()
When searching a data base for an object with a specific id, what method needs to be called an the id string?
new ObjectID()
What is an important difference between the findOne and the find method?
The findOne method takes a callback that returns the required data.
The find method has no callback and doesn’t return anya actual data, instead it returns a cursor.
in order to get the data from the cursor, one needs to call the toArray() method and pass in a callback function.
What is the advantage of having the find method return a cursor instead of the actual data?
It is much more memory efficient, especially when you don’t need the actual data, like when you only want a count of all the different entries that meet a specific criteria.