在Java中将MongoDB查询结果保存到不同的变量中

在Java中将MongoDB查询结果保存到不同的变量中,java,mongodb,Java,Mongodb,我有一个用Java编写的MongoDB查询,我正在尝试将不同的结果保存到变量中。例如,“Item”中的值应该保存为一个名为ItemName的字符串。“Seller”中的值保存到另一个变量中。我该怎么做呢 这是我的疑问 // Database Name MongoDatabase database = mongoClient.getDatabase("Marketplace"); // Collection name MongoCollection col

我有一个用Java编写的MongoDB查询,我正在尝试将不同的结果保存到变量中。例如,“Item”中的值应该保存为一个名为ItemName的字符串。“Seller”中的值保存到另一个变量中。我该怎么做呢

这是我的疑问

        // Database Name
    MongoDatabase database = mongoClient.getDatabase("Marketplace");

    // Collection name
    MongoCollection collection = database.getCollection("Sales");

    MongoCursor<Document> cursor = collection.find().iterator();
    try {
        while (cursor.hasNext()) {
            System.out.println(cursor.next().toJson());
        }
    } finally {
        cursor.close();
    }

对于自动映射,您可以使用例如

这是另一种选择

{"_id": {"$oid": "5e74ae1f1c410c6af0670d9b"}, "Item": "Soap", "Seller": "Antoine", "Size": "null", "Condition": "New", "BIN": "100", "BID": "50", "Link": "null"}