如何在java中从Mongo子文档中获取特定数据?
我试图获取mongo文档中特定列值的数据,但它显示了整个数据 以下是mongo文件:如何在java中从Mongo子文档中获取特定数据?,java,mongodb,Java,Mongodb,我试图获取mongo文档中特定列值的数据,但它显示了整个数据 以下是mongo文件: { "_id" : ObjectId("59db2321811a592384865711"), "User_ID" : "demo", "Project_ID" : "demo-1", "Project_Information" : { "Project_Description" : "Sample", "Primary_Building_Type
{
"_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"
}]
我尝试使用dot:项目信息。项目描述
。代码如下:
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();
}
public对象[]addDemo1(字符串用户ID)引发异常{
DB DB=ConnectToDB.getConnection();
Properties prop=新属性();
InputStream输入=null;
input=GetProjectStatus.class.getClassLoader().getResourceAsStream(“config.properties”);
道具载荷(输入);
String col=prop.getProperty(“COLLECTION_PI”);
System.out.println(“数据为..”+col);
DBCollection=db.getCollection(col);
BasicDBObject对象=新的BasicDBObject();
BasicDBObject字段=新建BasicDBObject();
BasicDBObject字段S2=新的BasicDBObject();
列表obj1=null;
如果(User_ID!=null&&!User_ID.equals(“”&&User_ID.length()>0){
obj.put(“用户ID”,用户ID);
字段。放置(“\u id”,0);
字段。put(“用户ID”,1);
字段。put(“项目ID”,1);
字段。追加(“项目信息。项目描述”,“项目描述”);
BasicDBObject字段1=新的BasicDBObject();
字段1.put(“用户ID”,用户ID);
}
DBCursor=collection.find(对象,字段);
System.out.println(“计数为:+cursor.count());
obj1=游标。toArray();
系统输出打印项次(“+obj1”);
cursor.close();
db.getMongo().close();
返回obj1.toArray();
}
但是它显示了项目信息的整个结构
请具体说明如何实现这一点。谢谢您的帮助。Java库不允许您使用dot直接访问
他们有内置的getter和setter方法
您没有提到您正在使用的软件包
以下是您需要的查询:
db.mycol.find({},{User_ID:1,"Project_Information.Project_Description":1})
它将提供:
{ "_id" : ObjectId("59db2321811a592384865711"),
"User_ID" : "demo",
"Project_Information" : { "Project_Description" : "Sample" }
}
您必须以包接受的任何格式转换查询
以下是一个教程:
Java库不允许您使用dot直接访问
他们有内置的getter和setter方法
您没有提到您正在使用的软件包
以下是您需要的查询:
db.mycol.find({},{User_ID:1,"Project_Information.Project_Description":1})
它将提供:
{ "_id" : ObjectId("59db2321811a592384865711"),
"User_ID" : "demo",
"Project_Information" : { "Project_Description" : "Sample" }
}
您必须以包接受的任何格式转换查询
以下是一个教程:
使用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());
}
// 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());
}
使用3.x MongoDB Java驱动程序
下面是一个使用MongoDB 3.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());
}
// 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());
}
//这将查找给定集合中的所有文档(注意:没有为find()调用提供参数)
//对于每个it项目的文档,请参见项目信息。项目描述
可查找文件=
mongoClient.getDatabase(“…”).getCollection(“…”)
.find()
//对于要投影的每个属性,必须包含其点表示法路径和值1。。。
//这相当于在MongoDB shell中指定{'Project_Information.Project_Description':1}
.项目(新文件(“项目信息.项目说明”,1));
用于(文档:文档){
//响应包含一个子文档,位于“项目信息”项下
文档项目信息=(文档)Document.get(“项目信息”);
//“项目描述”在本子文件中
String projectDescription=projectInformation.getString(“项目描述”);
//打印“样本”
System.out.println(项目描述);
//要在对象[]中返回此单个字符串值(如OP所示),只需像这样创建对象[],然后返回它。。。
对象[]r=新对象[]{projectDescription};
//打印整个投影文档,例如{“\u id”:{“$oid”:“59db221811a592384865711”},“项目信息”:{“项目描述”:“示例”}
System.out.println(document.toJson());
}
使用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());
}
// 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());
}
使用3.x MongoDB Java驱动程序
下面是一个使用MongoDB 3.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());
}
// 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());
}
//这将查找给定集合中的所有文档(注意:没有为find()调用提供参数)
//对于每个it项目的文档,请参见项目信息。项目描述
可查找文件=
mongoClient.getDatabase(“…”).getCollection(“…”)
.find()
//对于要投影的每个属性,必须包含其点表示法路径和值1。。。
//这相当于在MongoDB shell中指定{'Project_Information.Project_Description':1}
.项目(新文件(“项目信息.项目说明”,1));
用于(文档:文档){
//响应包含一个子文档,位于“项目信息”项下
文档项目信息=(文档)Document.get(“项目信息”);
//“项目描述”在本子文件中
String projectDescription=projectInformation.getString(“项目描述”);
//打印“样本”
System.out.println(项目描述);
//要在对象[]中返回此单个字符串值(如OP所示),只需像这样创建对象[],然后返回它。。。
对象[]r=新对象[]{projectDescription};
//打印整个投影文档,例如{“\u id”:{“$oid”:“59db221811a592384865711”},“项目信息”:{“项目描述”:“示例”}
System.out.println(document.toJson());
}
-1因为您没有包含任何尝试过的代码。怎么