gpt4 book ai didi

java - 如何在java中从Mongo子文档中获取特定数据?

转载 作者:太空宇宙 更新时间:2023-11-04 11:03:34 25 4
gpt4 key购买 nike

我尝试获取 mongo 文档中特定列值的数据,但它显示了整个数据。

以下是mongo文档:

{
"_id" : ObjectId("59db2321811a592384865711"),
"User_ID" : "demo",
"Project_ID" : "demo-1",
"Project_Information" : {
"Project_Description" : "Sample",
"Primary_Building_Type" : "Office",
"State" : "AR",
"Analysis_Type" : "1",
"Project_Billing_Number" : "WY",
"Country" : "USA",
"Climate_Zone" : "3A",
"Zip_Code" : "71611"
"City" : "WA",
"Units" : "IP"
}
}

我想获取以下输出:

[
{
"User_ID": "demo",
"Project_Description": "Sample"
}]

我尝试使用点:Project_Information.Project_Description。代码如下:

public Object[] addDemo1(String User_ID) throws Exception {
DB db = ConnectToDB.getConnection();
Properties prop = new Properties();
InputStream input = null;
input = GetProjectStatus.class.getClassLoader().getResourceAsStream("config.properties");
prop.load(input);
String col = prop.getProperty("COLLECTION_PI");
System.out.println("data is.." + col);
DBCollection collection = db.getCollection(col);
BasicDBObject obj = new BasicDBObject();
BasicDBObject fields = new BasicDBObject();
BasicDBObject fields2 = new BasicDBObject();
List<DBObject> obj1 = null;
if (User_ID != null && !User_ID.equals("") && User_ID.length() > 0) {
obj.put("User_ID", User_ID);
fields.put("_id", 0);
fields.put("User_ID", 1);
fields.put("Project_ID", 1);
fields.append("Project_Information.Project_Description", "Project_Description");
BasicDBObject fields1 = new BasicDBObject();
fields1.put("User_ID", User_ID);
}
DBCursor cursor = collection.find(obj, fields);
System.out.println("count is:" + cursor.count());
obj1 = cursor.toArray();
System.out.println("" + obj1);
cursor.close();
db.getMongo().close();
return obj1.toArray();

}

但它显示了Project_Information的整个结构。

请具体说明如何实现这一目标。感谢您的帮助。

最佳答案

使用 2.x MongoDB Java 驱动程序

以下是使用 MongoDB 2.x Java 驱动程序的示例:

DBCollection collection = mongoClient.getDB("stackoverflow").getCollection("demo");

BasicDBObject filter = new BasicDBObject();
BasicDBObject projection = new BasicDBObject();

// project on "Project_Information.Project_Description"
projection.put("Project_Information.Project_Description", 1);

DBCursor documents = collection.find(filter, projection);

for (DBObject document : documents) {
// the response contains a sub document under the key: "Project_Information"
DBObject projectInformation = (DBObject) document.get("Project_Information");
// the "Project_Description" is in this sub document
String projectDescription = (String) projectInformation.get("Project_Description");

// prints "Sample"
System.out.println(projectDescription);

// to return this single String value in an Object[] (as implied by your OP) just do create the Object[] like this and then return it ...
Object[] r = new Object[] {projectDescription};

// prints the entire projected document e.g.
// { "_id" : { "$oid" : "59db2321811a592384865711" }, "Project_Information" : { "Project_Description" : "Sample" } }
System.out.println(document.toString());
}

使用 3.x MongoDB Java 驱动程序

以下是使用 MongoDB 3.x Java 驱动程序的示例:

    // this finds all documents in a given collection (note: no parameter supplied to the find() call) 
// and for each document it projects on Project_Information.Project_Description
FindIterable<Document> documents =
mongoClient.getDatabase("...").getCollection("...")
.find()
// for each attrbute you want to project you must include its dot notation path and the value 1 ...
// this is the equivalent of specifying {'Project_Information.Project_Description': 1} in the MongoDB shell
.projection(new Document("Project_Information.Project_Description", 1));

for (Document document : documents) {
// the response contains a sub document under the key: "Project_Information"
Document projectInformation = (Document) document.get("Project_Information");
// the "Project_Description" is in this sub document
String projectDescription = projectInformation.getString("Project_Description");

// prints "Sample"
System.out.println(projectDescription);

// to return this single String value in an Object[] (as implied by your OP) just do create the Object[] like this and then return it ...
Object[] r = new Object[] {projectDescription};

// prints the entire projected document e.g. { "_id" : { "$oid" : "59db2321811a592384865711" }, "Project_Information" : { "Project_Description" : "Sample" } }
System.out.println(document.toJson());
}

关于java - 如何在java中从Mongo子文档中获取特定数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46647056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com