[英]MongoDB $lookup query returning empty field
There are two collections in my database, Projects
and Tasks
. 我的数据库中有两个集合:
Projects
和Tasks
。 Each project can have a number of tasks associated to it. 每个项目可以有许多与之相关的任务。 I am trying to create a MongoDB query in Java that will return a
Task
and also embed the Project
that is linked to it, however the code I have completed so far only returns an empty array (I have named this something
which appears as []
below). 我试图在Java中创建MongoDB的查询将返回一个
Task
,并且还嵌入Project
链接到它,但我迄今只完成了代码返回一个空数组(我命名这个something
它显示为[]
下面)。
Unfortunately I have not found many examples on how to properly use $lookup
in Java. 不幸的是,我没有找到许多有关如何在Java中正确使用
$lookup
示例。 What changes do I need to perform on the code so the something
field comes back with a project
? 我需要什么样的变化,因此对代码进行
something
外地回来了project
? Should I be using $lookup
or another aggregation operator? 我应该使用
$lookup
还是其他聚合运算符?
The Java method I am using with mongo-java-driver version 3.5.0: 我在mongo-java-driver版本3.5.0中使用的Java方法:
public static String readTask()
{
MongoClient mongoClient = new MongoClient();
MongoDatabase database = mongoClient.getDatabase("exampleDatabase");
Bson lookup = new Document("$lookup",
new Document("from", "Project")
.append("localField", "project._id")
.append("foreignField", "_id")
.append("as", "something"));
List<Bson> filters = new ArrayList<>();
filters.add(lookup);
AggregateIterable<Document> it = database.getCollection("Task").aggregate(filters);
System.out.println("First Document: " + it.first().toString());
return it.first().toString();
}
This method currently returns the following: 该方法当前返回以下内容:
{
_id = 599a62cac29d9a2684c64012,
constructionRoomNumber = 15,
type = Example,
summary = Summary Text,
description = Description Text,
status = Open,
project = {
"$ref": "Project",
"$id": "5996582a0983347784fb2ff4"
},
something = []
}
Expected Result: 预期结果:
{
_id: ObjectId('599a62cac29d9a2684c64012')
constructionRoomNumber: "15"
type: "Example"
summary: "Summary Text"
description: "Description Text"
status: "Open"
project: {
_id: ObjectId('5996582a0983347784fb2ff4')
projectCode: "V1000"
projectName: "Example Project"
projectLocation: "1 Somewhere Street, Some City"
clientName: "Whatever Client"
isActive: true
}
}
Here is an example of what the data stored in MongoDB looks like: 以下是存储在MongoDB中的数据的示例:
Example Project: 示例项目:
_id: ObjectId('5996582a0983347784fb2ff4')
projectCode: "V1000"
projectName: "Example Project"
projectLocation: "1 Somewhere Street, Some City"
clientName: "Whatever Client"
isActive: true
Example Task: 示例任务:
_id: ObjectId('599a62cac29d9a2684c64012')
constructionRoomNumber: "15"
type: "Example"
summary: "Summary Text"
description: "Description Text"
status: "Open"
project: DBRef(Project, 5996582a0983347784fb2ff4, undefined)
You are trying a lookup on a DBRef field, which is not supported. 您正在尝试在不支持的DBRef字段上进行查找。
However, there is a workaround to transform DBRefs into simple ObjectId that I have detailed on a different answer , you can adapt it in Java or use it in a native query. 但是,有一种解决方法可以将DBRef转换为简单的ObjectId,我已经在另一个答案中进行了详细介绍 ,您可以在Java中对其进行改编或在本机查询中使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.