MongoDB (part2): CRUD

Hang Nguyen
3 min readJun 9, 2022

Insert without order

When we used insertMany() method, we want to insert all documents into collection. By default, documents are added to collection in order of arrays. If any document has duplicate id for instance, then the whole insertation process ends right there. In this case, we can add instance {ordered:false} then the all documents will be inserted except for the document having error.

For instance:

insertMany([Doc1, Doc2, Doc3])

During the insertation process of Doc2, some errors occur -> only Doc1 was inserted and Doc2 & Doc3 were not inserted.

insertMany([Doc1, Doc2, Doc3], {ordered: false})

Now both Doc1 and Doc3 were inserted when Doc2 insertation process encounters problems.

Operator

Query and Projection Operators

Query operators provide ways to locate data within the database and projection operators modify how data is presented.

Update operator

Cursor

In MongoDB, when the find() method is used to find the documents present in the given collection, then this method returned a pointer which will points to the documents of the collection, now this pointer is known as cursor. Or in other words we can say that a cursor is a pointer, and using this pointer we can access the document. By default, cursor iterate automatically, but you can iterate a cursor manually which we will discuss later.

Cursor method

--

--

Hang Nguyen

A Data Engineer with a passion for technology, literature, and philosophy.