0

I am trying to insert data into MongoDB using java but I get an error when I try to compile the code. I have no idea what's causing the error. I want to add a new book entry.

error message:
 required: String,Object
  found: List<Document>
  reason: actual and formal argument lists differ in length
UseMongoDB.java:61: error: method put in class Document cannot be applied to given types;
book.put(publishers);

private MongoCollection<Document> books = db.getCollection("books");

	void insertanewbook() {
		
		
		Document book = new Document();
		book.put("title", "test");
	      	book.put("category","test");
                book.put("price",12.3);

                List<Document> authors = new ArrayList<Document>();
		Document author = new Document();
                author.put("first_name","jonn");
                author.put("last_name","james");
                author.put("country","t");
                author.put("website","www.test.com");
		authors.add(author);
		book.put(authors);

		List<Document> publishers = new ArrayList<Document>();
		Document publisher = new Document();
                publisher.put("publish_date",new Date());
                publisher.put("name","test");
                publisher.put("country","test");
		publisher.put("website","www.test.com");
		publishers.add(publisher);
		book.put(publishers);
                
		books.insertOne(book);

	}

1 Answer 1

1

From the error message you posted...

reason: actual and formal argument lists differ in length

In other words, method put requires two arguments and you are supplying only one. From the code you posted, you are missing the key in the call to method put(). Just add the relevant key, for example

book.put("publishers", publishers);
Sign up to request clarification or add additional context in comments.

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.