MongoDB notes

Who should read it: It is for you if you are looking for an overview of this topic for a project, to conduct/appear in an interview, or in general. As I learn more, we will update this article.

MongoDB is a document based, schema-less, and a highly available NOSQL database. Let’s look at some high level details about it.

Basic Terms:

  • document: is a basic unit of data equivalent of a row in an RDBMS table.
  • collection: is equivalent to a table in an RDBMS database.
  • database: A single MongoDB instance can have multiple instances of databases.
  • mongo shell: it’s a shell that helps in administrative tasks.

Basic Operations:

CREATE:

  • Use insertOne() operation.
  • If we need to insert many documents into a collection, use insertMany().
  • MongoDB also provide a Bulk Write API, that affects a single collection. It has db.collection.bulkWrite() method which by default performs ordered operations. It also has an option to turn off ordering.

READ:

  • Use find() or findOne() operation.

DELETE:

  • Use deleteOne() to delete a single document.
  • Use deleteMany() to delete all eligible matching documents.

UPDATE:

  • For update, we can use updateOne(), updateMany(), or replaceOne().
  • updateOne() and updateMany() takes a filter document and a modifier document, as the second parameter.
  • replaceOne() expects a document with which it will replace the document with the first input document.

DROP:

  • It is possible to use deleteMany() to drop all matching documents in a collection. It can also be used to delete all documents in a collection.
  • To clear an entire collection, use of drop() is faster.

UPSERT:

  • It is equivalent to update-else-insert. Means, update a matching document. If no matching document found, insert a new document.

Supported Data types: Null, Boolean, Number, String, Date, Regular expression, Array, Embedded document, Object ID, Binary Data, Code

ACID operations: ACID (Atomicity, Consistency, Isolation, Durability) operations in MongoDB are at a document level. Below are some basics of ACID operations:

  • Atomicity: zero or none operations.
  • Consistency: ensure data is persisted in all nodes.
  • Isolation: Any reads or writes are not impacted with other reads or writes.
  • Durability: Commits are performed successfully.

References:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s