0

Currently, can not put "insertOne" due to maybe current jar. *I am using "mongo-java-driver-3.12.2.jar" on IntelliJ.

Would you mind telling me what jar file does include "insertOne" ?

What I'm trying to do is below.

    MongoClientURI uri = new MongoClientURI(
                    "mongodb+srv://xxxx");

            try(MongoClient mongoClient = new MongoClient(uri)){
                MongoDatabase database = mongoClient.getDatabase("test");
                MongoCollection<Document> collection = database.getCollection("test");
                Document query = new Document("_id", new ObjectId("objectID xxxx"));
                Document result = collection.find(query).iterator().next();

                Document test = new Document("_id","ObjectID xxxx");
                test.append("test4","test is 4");
                query.insertOne(test);  --> Currently the "insertOne" is not recognized..


                System.out.println("Test3: "+result.getString("test3"));
3
  • 1
    You are trying to perform an insertOne on a Document object. That doesn't make sense. You insert on a MongoCollection. See the guide here: mongodb.github.io/mongo-java-driver/3.4/driver/getting-started/… Commented Mar 12, 2020 at 16:07
  • Thank you very much! I could not find that. In terms of "Collection.insertOne", this is used in case of adding. what about updating the existing one? I could not find that description. Commented Mar 12, 2020 at 18:17
  • Here are some tutorials for doing CRUD (create, read, update and delete) operations using the Java and MongoDB: Quick Start: Java and MongoDB. Commented Mar 13, 2020 at 1:14

1 Answer 1

2

I see you're trying to do query.insertOne(test), but you defined query as being a Document. However, the insertOne method is defined on a MongoCollection, not on a Document, see the documentation

Instead, try: collection.insertOne(test)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I could not find what you attached. Now I got it !

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.