简体   繁体   中英

inserting data into mongodb using java

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); } 

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM