MongoDB – Create Documents

– “ordered”: false

if _id is not provided, MongoDB inserts unique key.
if MongoDB founds duplicate values, it stops inserting other records. It follows insertions in the same order.
To prevent stopping at first error, we need to add attribute { “ordered”: false } to continue with others.
in batch inserts, if all are a success, it returns a list of ids.
in the case of errors, it shows success records counts and list of errors.

Example one:
db.moviesScratch.insertOne({ “title”: “Movie 1”, “year”: “1976”, “imdb”: “tt0075148”});
db.moviesScratch.insertOne({ “_id”: “tt0075148”, “title”: “Movie 1”, “year”: “1976” });

When we search by tt0075148, it returns both documents. Because both are having different _ids and it search entire document.

“_id” : “tt0084726”,
“title” : “Movie 1”,
“year” : 1982,
“type” : “movie”
“_id” : “tt0796366”,
“title” : “Movie 2”,
“year” : 2009,
“type” : “movie”
“_id” : “tt0117731”,
“title” : “Movie 3”,
“year” : 1996,
“type” : “movie”
“ordered”: false



Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s