我正在尝试使用 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);
添加回答
举报
0/150
提交
取消