如何使用MongoDB Java向字典添加项?

如何使用MongoDB Java向字典添加项?,java,mongodb,dictionary,mongodb-java,Java,Mongodb,Dictionary,Mongodb Java,我有以下几点。我发明了一本字典 Dictionary<String, String> shifts = new Hashtable(); 然后我尝试向字典中添加项,但它要么给我null,要么什么也没有 MongoCollection<Document> collection = db.getCollection("history"); UpdateResult updateResult = collection.updateOne(eq("

我有以下几点。我发明了一本字典

Dictionary<String, String> shifts = new Hashtable();
然后我尝试向字典中添加项,但它要么给我null,要么什么也没有

MongoCollection<Document> collection = db.getCollection("history");
UpdateResult updateResult = collection.updateOne(eq("_id", "12345"), push("data", shifts.put(c.getDateTime(), minutesWorked())));
MongoCollection collection=db.getCollection(“历史”);
UpdateResult UpdateResult=collection.updateOne(eq(“_id”,“12345”),push(“数据”,shift.put(c.getDateTime(),minutesWorked());
如何更新/向词典中添加项目?

(1)您不能将数据推入
词典
。(2) 不要使用
词典
,它是一个遗留集合,已过时;尝试使用
Map
界面和
HashMap
。(3) 对于您的情况,最好使用
列表
集合。使用shift对象,以便数据作为shift对象数组存储在MongoDB集合中。然后,可以将shift对象推入shift数组。此外,您可能需要构建一个Pojo类,定义您试图存储在集合中的数据,并将移位作为列表。(4) 但是,您也可以使用可用的
Document
类来代替Pojo对象。
MongoCollection<Document> collection = db.getCollection("history");
UpdateResult updateResult = collection.updateOne(eq("_id", "12345"), push("data", shifts.put(c.getDateTime(), minutesWorked())));