0

In C# we can bulk insert data to mongo using insertBatch() method

public string SendDatatoMongo()
    {
        string result = "";
        using (TestEntities context = new TestEntities())
        {
            var connectionString = "mongodb://myserver:27017/?safe=true;w=1;wtimeout=3000s";
            MongoServer server = MongoServer.Create(connectionString);
            try
            {
                  var data = context.mytable.OrderBy(x => x.Id).Skip(30000000).Take(100000).AsQueryable();
                  MongoDatabase db = server.GetDatabase("mydb");
                  MongoCollection MCollection = db.GetCollection("mytable");
                  MCollection.InsertBatch(data);

            }
            catch (Exception ex)
            {
                result = ex.Message;

            }
            finally { server.Disconnect(); }
        }
        return result;
    }

Can we do the same thing in java? I didn't find any batch insert api using mongo java driver. I am trying to take data in bulk from sqlserver and insert in mongo. Its directly insertion from sqlserver to mongo not by creating any document in between.

        Class.forName(driver).newInstance();
        conn = DriverManager.getConnection(url, databaseUserName, databasePassword);
        stmt =  conn.prepareStatement("select  * from mytable where Id > 2000000);
        result = null;
        result =   stmt.executeQuery(); 
       // stmt.addBatch();
        result.setFetchSize(1000);
         while (result.next()) {
            //System.out.println("Inside while");
            mybean bean = new mybean();
            bean.setId(result.getLong(("Id")));
            List<DBObject> list = new ArrayList<DBObject>();
            list.add(bean);
            MongoDBJDBC.dbconnection(list);
       }

This is what I am trying in java.

sangita
  • 191
  • 5
  • 14
  • possible duplicate of [How to insert multiple documents at once in mongodb through java](http://stackoverflow.com/questions/18128490/how-to-insert-multiple-documents-at-once-in-mongodb-through-java) – chridam Mar 18 '15 at 11:00
  • Nope. Please read the question before commenting. – sangita Mar 18 '15 at 11:11
  • 2
    It looks like the same problem as the question linked in chridam's comment - could you explain what about your question is different from how to do bulk insert in Java? Does it have to do with "directly insertion from sqlserver to mongo not by creating any document in between"? What does that mean? You need to create a document (DBObject) to insert into MongoDB. – wdberkeley Mar 18 '15 at 14:21

0 Answers0