Thursday 19 February 2015

mongodb : Insert one or more documents


You can insert a document into mongoDB using insert or save method.

Insert document using insert method
Syntax
db.collection.insert(
   <document or array of documents>,
   {
     writeConcern: <document>,
     ordered: <boolean>
   }
)


Parameter
Type
description
document
document or array
A document or array of documents to insert into the collection.
writeConcern
document
Optional. I will explain this in later posts
ordered
boolean
Optional. Default value is true.
If true, perform an ordered insert of the documents in the array, and if an error occurs with one of documents, MongoDB will return without processing the remaining documents in the array.
If false, perform an unordered insert, and if an error occurs with one of documents, continue processing the remaining documents in the array.



If collection don’t exist, then insert method creates a collection at the time of insertion.

_id field
If document don’t specify _id field at the time of insertion, then MongoDB will add the _id field and assign a unique ObjectId for the document before inserting. The _id value is unique in the collection.

ObjectId is a 12-byte BSON type, constructed using:

    a 4-byte value representing the seconds since the Unix epoch,
    a 3-byte machine identifier,
    a 2-byte process id, and
    a 3-byte counter, starting with a random value.

> db.employee.insert({"id":1,"firstName":"Hari Krishna","lastName":"Gurram"})
WriteResult({ "nInserted" : 1 })
>
> db.employee.find()
{ "_id" : ObjectId("54b2a8c9dbf1847bed465758"), "id" : 1, "firstName" : "Hari Krishna", "lastName" : "Gurram" }
>


In the above snippet, I insert a document into employee collection without specifying “_id” field, so mongoDB creates id for the document. “_id” field is primary key field for the document, it is used to uniquely identify the document. If you don’t specify any value for “_id” field, then mongoDB generates one for the document.

Insert document by specifying _id field
> db.employee.insert({"_id" : 2, "firstName":"Rama Krishna","lastName":"Gurram"})
WriteResult({ "nInserted" : 1 })
>
> db.employee.find()
{ "_id" : ObjectId("54b2a8c9dbf1847bed465758"), "id" : 1, "firstName" : "Hari Krishna", "lastName" : "Gurram" }
{ "_id" : 2, "firstName" : "Rama Krishna", "lastName" : "Gurram" }
>


Insert array of documents
> db.employee.insert(
 [
   {
     "id": 1,
     "firstName": "Joel",
     "lastName": "chelli"
   },
   {
     "id": 2,
     "firstName": "Ananad",
     "lastName": "Bandaru"
   },
   {
     "id": 3,
     "firstName": "Gopi",
     "lastName": "Battu"
   },
   {
     "id": 4,
     "firstName": "Ritwik",
     "lastName": "Mohenthy"
   }
 ]
 )
BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 4,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})
>
> db.employee.find()
{ "_id" : ObjectId("54b2a8c9dbf1847bed465758"), "id" : 1, "firstName" : "Hari Krishna", "lastName" : "Gurram" }
{ "_id" : 2, "firstName" : "Rama Krishna", "lastName" : "Gurram" }
{ "_id" : ObjectId("54b2aa07dbf1847bed465759"), "id" : 1, "firstName" : "Joel", "lastName" : "chelli" }
{ "_id" : ObjectId("54b2aa07dbf1847bed46575a"), "id" : 2, "firstName" : "Ananad", "lastName" : "Bandaru" }
{ "_id" : ObjectId("54b2aa07dbf1847bed46575b"), "id" : 3, "firstName" : "Gopi", "lastName" : "Battu" }
{ "_id" : ObjectId("54b2aa07dbf1847bed46575c"), "id" : 4, "firstName" : "Ritwik", "lastName" : "Mohenthy" }
>

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment