MongoDB – CRUD Operations

MongoDB is a cross-platform, document oriented database that provides, high performance, high availability, and easy scalability.

Quick Command reference:

CRUD operations

Creating documents:

MongoDB is able to provide few options to import documents into collection: insertOne, insert, update (upsert) and save.

output:

  • A boolean acknowledged as true if the operation ran with write concern or false if write concern was disabled.

If you want to add multiple documents at once, MongoDB provides insertMany with ordered (default – it will stop at first error) or unordered mode – even there are errors MongoDB will continue the insert with the next document.

output:

[code language=”javascript”][/code]

Update with upsert = true

If set to true, creates a new document when no document matches the query criteria. The default value is false, which does not insert a new document when no match is found.

You can use save command to quick update/insert a document into collection:

Reading documents:

Cursors:

cursor.objsLeftInBatch() returns the number of objects remaining in batch.

Logical operators:

Array match:

Update operators:

where optional options_document has any one or more of the following optional parameters:

Example:

See https://docs.mongodb.org/manual/reference/operator/update/

Delete:

Data aggregation:

Please follow this link with additional info about Data Aggregation.

Users management:

Reduce function:

and the output:

Pattern matching with $regex = equivalent to “like %” in RDBMS

Pattern matching without $regex:

Map-Reduce:

Map-Reduce is a data processing paradigm for condensing large volumes of data into useful aggregated results.

map-reduce

Source: mongodb

Additional links:1.MongoDB card reference.pdf