insert document in mongodb

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

In previous article we saw how you can connect to mongodb using java. This article will explain how you can insert document in mongodb.

Insert Document In MongoDB

Mongodb stores documents in bson form. Just like we use json like structure to represent data, we can populate data in a bson Document object and then simply put in collection. Let us directly turn to an example to insert document in mongodb.

Create Mongodb connection

Create mongodb connection from java program.

Retrieve Database and Collection

Mongodb driver provides the api to connect to  MongoDatabase and MongoCollection. Code snippet below will connect to employee_db database and retrieve the employee collection in which we want to insert document in mongodb.

Populate Data in Document

Consider you want to insert document in mongodb as follows.

We can populate above data using java code below.

and insert document in mongodb using

Complete Example

Run this example and you would see the result as below

I hope this article helped understand how to insert document in mongodb using java.

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

9 thoughts on “insert document in mongodb

  • Pingback:retrieve documents from mongodb using java - theJavaGeek

  • February 21, 2017 at 3:57 pm
    Permalink

    Sir, Kindly tell me how we can store images in MonogDB and fetch in servlet.

    Thank you.

    Reply
    • February 22, 2017 at 11:15 am
      Permalink

      hi, I think it would be better if only paths are stored in mongodb and images stored in hard drive. With path, you can pick images from hard drive

      Reply
  • March 9, 2017 at 4:33 pm
    Permalink

    Hi i hav data of 6 rows in json structure format but when i insert into mongodb only 1st record is stored how to insert json data as it is with multiple rows please help me am realy stucked

    Reply
  • March 27, 2017 at 12:52 pm
    Permalink

    Thanks you sir, for your information related java – Mongodb database connectivity

    Reply
  • August 24, 2017 at 8:57 pm
    Permalink

    hello sir ,
    i have problem in statement for MongoDatabase db=client.getDatabase(“crawler”);

    MongoCollection coll=db.getCollection(“enduser”);

    Document doc=new Document();
    give me error for all operation

    Reply
  • March 21, 2018 at 9:45 am
    Permalink

    Hi,
    the code that you posted. It does not allow me to use int values with put method.
    Gives me this error:
    The method put(String, Object) in the type Document is not applicable for the arguments (String, int)
    eg, doc1.put(“likes”, 1000);
    How did you execute it ?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *