0

I am new to MongoDb. I am trying to insert the data in MongoDb using java. Everything works fine but when i try to use insert() function to insert the data error is shown. It says change type of documents to DBObject[].

Database.java

package database;

import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
public class Database 
{

    public static void main(String args[])
    {
        MongoClient mongo= new MongoClient("localhost",80);
        DB db = mongo.getDB("Collection");
        DBCollection group=db.getCollection("Group");
        BasicDBObject documents= new BasicDBObject();
        documents.put("saf", "ad");

        group.insert(documents);//error is shown here
    }
}
4
  • Please copy and paste the exact error message that you get. It contains important information about what's wrong. Commented Dec 8, 2015 at 8:22
  • it simply says changetype of documents to DBObject[]. Commented Dec 8, 2015 at 8:28
  • 1
    What version of MongoClient are you using? It looks like you are using an old version that did not yet have the insert method that takes varargs. Use a newer version of MongoClient. Commented Dec 8, 2015 at 8:29
  • I am using mongo-java-driver-2.2.0.jar Commented Dec 8, 2015 at 8:35

3 Answers 3

3

BasicDBObject as subsets of DBObject. Also to save the BasicDBObject we have a call as save method

group.save(documents);
Sign up to request clarification or add additional context in comments.

2 Comments

A cast is not necessary, because BasicDBObject is a subtype of DBObject.
My bad I have updated my code also we can save the BasicDBObject. As there is a method for that
0

Your Code is correct and runs as is. Just update the mongo-java driver to version 2.13.0 from your existing one. Download link:

JavaDriver2.13

or: All Mongo Drivers

2 Comments

It says to change to DBObject[]
I tried running your code as a junit test and it was fine,just added throws UnknownHostException to the method..
0

You are using .insert() on DBCollection.

public WriteResult insert(DBObject... arr)
                   throws MongoException
Saves document(s) to the database. if doc doesn't have an _id, one will be added you can get the _id that was added from doc after the insert
Parameters:
arr - array of documents to save
Throws:
MongoException 

Check JavaDoc for insert. This is expecting an array of DBObject.

That's why the error:

change type of documents to DBObject[]

As @Vivek mentioned, you can use .save() method:

public final WriteResult save(DBObject jo)
Saves an object to this collection.
Parameters:
jo - the DBObject to save will add _id field to jo if needed

Check Javadoc for save.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.