I cannot insert into Mongodb using Java

Asked

Viewed 506 times

4

I’m new in Mongo, I’m trying to make an Insert in the bank but it’s a problem when inserting

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;

    public class InsertDriver {
        public static void main(String[] args) throws UnknownHostException {
            DB dB = (new MongoClient("localhost", 27017)).getDB("realdatabase");
            DBCollection dBCollection = dB.getCollection("Channel");
            BasicDBObject basicDBObject = new BasicDBObject();
            basicDBObject.put("name", "guilherme");
            basicDBObject.put("state", "happy");
            dBCollection.insert(basicDBObject);

        }
    }

Error:(14, 21) java: no suitable method found for Insert(com.mongodb.Basicdbobject) method com.mongodb.Dbcollection.Insert(com.mongodb.Dbobject...) is not applicable (argument Mismatch; com.mongodb.Basicdbobject cannot be converted to com.mongodb.Dbobject[]) method com.mongodb.Dbcollection.Insert(java.util.List) is not applicable (argument Mismatch; com.mongodb.Basicdbobject cannot be converted to java.util.List)

1 answer

5


The installation to work would be two .jar:

Netbeans Development Environment


Code worked after installing these two packages:

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;

MongoClient mongoClient = new MongoClient("localhost", 27017);
DB db = mongoClient.getDB("realdatabase");        
DBCollection collection = db.getCollection("channel");
BasicDBObject basicDBObject = new BasicDBObject();
basicDBObject.put("name", "guilherme");
basicDBObject.put("state", "happy");
collection.insert(basicDBObject);

only that the method getDB is obsolete, and soon will not work. Then use the method getDatabase,MongoDatabase and MongoCollection<Document> which are the most current.

import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import org.bson.Document;

MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", 27017));
MongoDatabase database = mongoClient.getDatabase("realdatabase");                
MongoCollection<Document> collection = database.getCollection("channel");

Document doc = new Document();
doc.put("name", "somalia");
doc.put("state", "casado");

collection.insertOne(doc);

Intellij IDEA development environment


There are also two .jar

Installation reference

Observing: There are therefore differences between Environments development and installed packages and their particularities.

  • even installing the two . jar has not yet worked

  • @Guilhermelima the code is tested in netbeans, what could be maybe there is the problem, You can edit your question and put the error log or what is not working please?

  • done, I’m using the intellij IDE

  • @Guilhermelima he is saying mixed type, you are sending a simple data and he is asking for a list of this given by error.! The funny thing about it is that it has both forms

  • @Guilhermelima which of the routines you are using I passed two?

  • I am using the first option that uses getDB, I tried the other also did not work

  • Let’s go continue this discussion in chat. @Guillhermelima

  • @Guilhermelima the solution is there for IDEA to remove those .jar and add these two in the links referencing the answer. Now it will work and give me a feedback! Edit the answer is at the end ...

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.