sql >> Base de Datos >  >> NoSQL >> MongoDB

Java:¿Cómo insertar un hashmap en MongoDB?

Use for loop para mapear el _id y los valores y recopilar todos los valores en la lista de documentos.

Algo como

Map<String, List<String>> inMap =  new HashMap<>();
  List<Document> documents = new ArrayList<>();
  for(Map.Entry<String, List<String>> kv :inMap.entrySet()) {
     Document doc = new Document();
     doc.put("_id", kv.getKey());
     List<String> values = kv.getValue();
     doc.put("query", values.get(0));
            ... rest of values
     documents.add(doc);
  }
collection.insertMany(documents);