亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用java將數據插入mongodb

使用java將數據插入mongodb

qq_笑_17 2023-05-24 15:04:00
我正在嘗試使用 java 將數據插入 MongoDB,但在嘗試編譯代碼時出現錯誤。我不知道是什么導致了錯誤。我想添加新書條目。error message: required: String,Object  found: List<Document>  reason: actual and formal argument lists differ in lengthUseMongoDB.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 回答

?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

從您發布的錯誤消息...

reason:?actual?and?formal?argument?lists?differ?in?length

換句話說,方法put需要兩個參數,而您只提供一個。從您發布的代碼中,您缺少對方法調用的關鍵put()。只需添加相關密鑰,例如

book.put("publishers",?publishers);


查看完整回答
反對 回復 2023-05-24
  • 1 回答
  • 0 關注
  • 175 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號