我正在尝试检索mongo集合中所有文档和嵌入文档的所有字段。这是一个文档的示例:
{
"_id": {
"$oid": "53b309def468718462e0c390"
},
"customer": {
"companyName": "Vins et alcools Chevalier",
"contactName": "Paul Henriot",
},
"employee": {
"employeeId": 5,
"firstName": "Steven",
"lastName": "Buchanan"
},
"orderItems": [
{
"productName": "Queso Cabrales",
"unitPrice": 14,
"quantity": 12
},
{
"productName": "Singaporean Hokkien Fried Mee",
"unitPrice": 9.8,
"quantity": 10
}
],
"orderId": 10248,
"orderDate": {
"$date": "1996-07-04T07:00:00.000Z"
},
"requiredDate": {
"$date": "1996-08-01T07:00:00.000Z"
},
"shippedDate": {
"$date": "1996-07-16T07:00:00.000Z"
},
"shipVia": "Federal Shipping",
"freightCost": 32.38
}
以下是我的代码和输出:
MongoCursor<Document> cursor;
MongoCollection<Document> collection = database.getCollection("test");
cursor = collection.find().iterator();
try {
while (cursor.hasNext()) {
//System.out.println(cursor.next().toJson());
System.out.println(cursor.next().get("key"));
}
} finally {
cursor.close();
}
这就是输出:
null null null null null ...
我只想要名字部分,但没用。
我在mongodbv4.2中使用java驱动程序3.12.4
给你加油。
1条答案
按热度按时间r9f1avp51#
试试这个: