使用java将数据插入mongodb

我正在尝试使用 java 将数据插入 MongoDB,但在尝试编译代码时出现错误。我不知道是什么导致了错误。我想添加新书条目。


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


  }


qq_笑_17
浏览 136回答 1
1回答

摇曳的蔷薇

从您发布的错误消息...reason: actual and formal argument lists differ in length换句话说,方法put需要两个参数,而您只提供一个。从您发布的代码中,您缺少对方法调用的关键put()。只需添加相关密钥,例如book.put("publishers", publishers);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java