w5 - mongoDB Flashcards
mongoDB
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
Document DB?
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
difference between mongoDB and SQL?
SQL MongoDB
Database Database
Table Collection
Index Index
Row Document
Column Field
Diff b/w mongoDB and JSON?
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.
MongoDB id?
A primary key is mandated in MongoDB and has the reserved field name _id.
Benefits of compass MongoDB?
Visually explore your data.
Run ad hoc queries in seconds.
Interact with your data with full CRUD functionality
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’);
}
});
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
Create
db.collection(‘week5table’).insertOne({name: ‘Tim’});
db.collection(‘week5table’).insertMany([
{ name: ‘Alex’, age: 25 },
{ name: ‘John’, age: 34 },
{ name: ‘Max’, age: 26 }
]);
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
Read
findOne
find()
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);
});
QUERY
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);
});
get all the documents where the name starts with ‘T’?
let query = { name: /^T/ };
db.collection(“week5table”).find(query).toArray(function (err, result) {
if (err) throw err;
console.log(result);
});
Finds all the documents where the name ends with ‘x’:
let query = { name: /x$/ };
db.collection(“week5table”).find(query).toArray(function (err, result) {
if (err) throw err;
console.log(result);
});
$gte
$lte
let query = { age: { $gte: 25 } };
db.collection(“week5table”).find(query).toArray(function (err, result) {
if (err) throw err;
console.log(result);
});
The list of Operators:
$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.
Sorting result?
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