FIT2095: Section 4
Creating or inserting documents CRUD
InsertOne(document) InsertMany(document,document)
How does mongoDB store data
It stores data in JSON-like documents, which means fields can vary from document to document and data structure can be changed over time
MongoDB documents are similar to
JSON objects
Does mongoDB require to define a schema for a collection
No
MongoDB and benefits
NoSQL open-source, cross-platform document-oriented database program that uses JSON-like documents with schemas. - offers high scalability, availability, and flexibility.
What is the only requirement in MongoDB document
The only requirement is that all documents in a collection must have a unique id, but the actual documents may have completely different fields.
Compass key features
Visually explore your data. Run ad hoc queries in seconds. Interact with your data with full CRUD functionality. View and optimize your query performance. Available on Linux, Mac, or Windows. Compass empowers you to make smarter decisions about indexing, document validation, and more.
When do we use .toArray when performing CRUD operations using plain MongoDB
When there are multiple results that result from retrieving documents from the database. It stores the results in an array e.g. find, findMany,
sort({name: 1})
ascending
delete documents
deleteOne(condition) deleteMany(condition)
sort({name: -1})
descending
MongoDB is a what database
document database, which means that the equivalent of a record is a document or an object.
Retrieve documents
findOne(filter) findMany(filter)
Const mongodb = require('mongodb'); Const MongoClient = mongodb.MongoClient MongoClient.connect(p1,p2,p3)
p1: MongoDB server URL p2: useNewUrlParser: true, required for the latest version of mongo p3: callback function (print err if error occurs or success if connected) function(err,client)
updateOne(p1,p2,p3)
p1: filter p2: modifications to apply p3: options to update the method such as upsert
Default mongoDB port
port 27017
update documents
updateOne(filter, update, option) updateMany(filter, update, option)
Benefits of Mongo DB
High performance High availability - MongoDB's replication facility, called a replica set, provides: automatic failover data redundancy. Horizontal scalability - MongoDB provides horizontal scalability as part of its core functionality: - Sharding distributes data across a cluster of machines. - Supports creating zones of data based on the shard key. Indexing Supports Ad-Hoc queries Rich query language
MongoDB vs. JSON objects
Compared to a JSON object, a MongoDB document has support not only for the primitive data types boolean, numbers, and strings, but also other common data types such as dates, timestamps, regular expressions, and binary data.
Match the term in mongoDB Database: Table: Index: Row: Column:
Database Collection Index Document Field