Insert Documents into MongoDB

Shahriar Shovon

Supported OS

  • All

Categories

Share this post with your friends

In this post, I will show you how to insert new documents into a collection. Checkout my previous post if you haven't, Create Databases and Collections in MongoDB

Inserting Single Document

Let's say you want to insert the following document into the users collection. { "_id": 1, "firstName": "Chrissie", "lastName": "Antony", "email": "cantony0@cdbaby.com", "gender": "Male", "ipAddress": "3.221.84.103" } NOTE: The data is generated with an online fake data generator mockaroo To insert the data you can use db.collectionName.insert(jsonDoc) where collection name is users and jsonDoc is the document you want to insert. Run the following command in mongo shell: db.users.insert({ "_id": 1, "firstName": "Chrissie", "lastName": "Antony", "email": "cantony0@cdbaby.com", "gender": "Male", "ipAddress": "3.221.84.103" }); You can make everything a lot more cleaner if you store the JSON document in a variable and pass that variable in db.users.insert var userData = { "_id": 1, "firstName": "Chrissie", "lastName": "Antony", "email": "cantony0@cdbaby.com", "gender": "Male", "ipAddress": "3.221.84.103" }; db.users.insert(userData); Once you run the command, you should see the following message: WriteResult({ "nInserted" : 1 }) Which means insertion is successful and nInserted: 1 means only 1 document was inserted. You can use db.collectionName.find() to get the documents in a collection. db.users.find(); { "_id" : 1, "firstName" : "Chrissie", "lastName" : "Antony", "email" : "cantony0@cdbaby.com", "gender" : "Male", "ipAddress" : "3.221.84.103" }

Inserting Multiple Documents

You can use insert() function to insert multiple documents as well. All you have to do is, store the documents as an array and pass that to the insert(), that's all. var usersData = [{ "_id": 2, "firstName": "Hedwig", "lastName": "Brannon", "email": "hbrannon0@ucla.edu", "gender": "Female", "ipAddress": "56.153.31.145" }, { "_id": 3, "firstName": "Dante", "lastName": "Marousek", "email": "dmarousek1@loc.gov", "gender": "Male", "ipAddress": "71.142.122.182" }, { "_id": 4, "firstName": "Clim", "lastName": "Old", "email": "cold2@mapquest.com", "gender": "Male", "ipAddress": "85.156.201.39" }]; db.users.insert(usersData); You should see the following output: BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 3, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) Notice the nInserted property. it has the value 3, which means, 3 documents were inserted. db.users.find(); { "_id" : 1, "firstName" : "Chrissie", "lastName" : "Antony", "email" : "cantony0@cdbaby.com", "gender" : "Male", "ipAddress" : "3.221.84.103" } { "_id" : 2, "firstName" : "Hedwig", "lastName" : "Brannon", "email" : "hbrannon0@ucla.edu", "gender" : "Female", "ipAddress" : "56.153.31.145" } { "_id" : 3, "firstName" : "Dante", "lastName" : "Marousek", "email" : "dmarousek1@loc.gov", "gender" : "Male", "ipAddress" : "71.142.122.182" } { "_id" : 4, "firstName" : "Clim", "lastName" : "Old", "email" : "cold2@mapquest.com", "gender" : "Male", "ipAddress" : "85.156.201.39" } These are all the documents that we have inserted till now.

The _id property

In my earlier examples, I included the _id property. If you leave it out, MongoDB will insert an ObjectId() as the _id for the document. Let's try inserting a new document without an _id property. var userData = { "firstName": "Miguela", "lastName": "Larrad", "email": "mlarrad0@zdnet.com", "gender": "Female", "ipAddress": "134.40.52.86" }; db.users.insert(userData); db.users.find(); { "_id" : 1, "firstName" : "Chrissie", "lastName" : "Antony", "email" : "cantony0@cdbaby.com", "gender" : "Male", "ipAddress" : "3.221.84.103" } { "_id" : 2, "firstName" : "Hedwig", "lastName" : "Brannon", "email" : "hbrannon0@ucla.edu", "gender" : "Female", "ipAddress" : "56.153.31.145" } { "_id" : 3, "firstName" : "Dante", "lastName" : "Marousek", "email" : "dmarousek1@loc.gov", "gender" : "Male", "ipAddress" : "71.142.122.182" } { "_id" : 4, "firstName" : "Clim", "lastName" : "Old", "email" : "cold2@mapquest.com", "gender" : "Male", "ipAddress" : "85.156.201.39" } { "_id" : ObjectId("59aad8292e418557742507bb"), "firstName" : "Miguela", "lastName" : "Larrad", "email" : "mlarrad0@zdnet.com", "gender" : "Female", "ipAddress" : "134.40.52.86" } Take a look at the last document. The "_id" : ObjectId("59aad8292e418557742507bb") property. It's an unique auto generated hex string called ObjectId I recommend you use the MongoDB's ObjectID instead of your own. Because that makes everything easier for the future.