我在mongodb 4.0中使用Spring(boot)数据2.2.7。我已经设置了3个试图通过聚合查找操作加入的集合。
目录
{
"_id" : ObjectId("5ec7856eb9eb171b72f721af"),
"model" : "HX711",
"type" : "DIGITAL",
....
}
映射者
@Document(collection = "catalog")
public class Product implements Serializable {
@Id
private String _id;
@TextIndexed
private String model;
....
股票
{
"_id" : ObjectId("5ec78573b9eb171b72f721ba"),
"serialNumber" : "7af646bb-a5a8-4b86-b56b-07c12a625265",
"bareCode" : "72193.67751691974",
"productId" : "5ec7856eb9eb171b72f721af",
......
}
映射者
@Document(collection = "stock")
public class Component implements Serializable {
@Id
private String _id;
private String productId;
....
在productId参数字段表示_id一个目录集合
运作
{
"_id" : ObjectId("5ec78671b9eb171b72f721d3"),
"componentId" : ""5ec78573b9eb171b72f721ba",
.....
}
映射者
public class Node implements Serializable {
@Id
private String _id;
private String componentId;
....
在COMPONENTID场是指_id一个股票集合
我想查询操作或库存集合以检索Product.model字段(在目录集合中)排序的相应Node或Component对象列表。
虽然目标是使用Java编写代码,但我尝试首先在Mongo shell中发出请求,但由于尝试将对象ID与字符串连接在一起(查找),所以我什至无法使它正常工作:Node.componentId- > Component._id Component.productId-> Product._id
对于关系Component(stock)-> Product(Catalog),我尝试了
LookupOperation lookupOperation = LookupOperation.newLookup()
.from("catalog")
.localField("productId")
.foreignField("_id")
.as("product");
TypedAggregation<Component> agg =
Aggregation.newAggregation(
Component.class,
lookupOperation
);
AggregationResults<Component> results = mongoTemplate.aggregate(agg, "stock", Component.class);
return results.getMappedResults();
但是它将返回没有产品信息的整个组件记录。
[{"_id":"5ec78573b9eb171b72f721b0","uuId":"da8800d0-b0af-4886-80d1-c384596d2261","serialNumber":"706d93ef-abf5-4f08-9cbd-e7be0af1681c","bareCode":"90168.94737714577","productId":"5ec7856eb9eb171b72f721a9","created":"2020-05-22T07:55:31.66","updated":null}, .....]
谢谢你的帮助。
注意:除了@Valijon答案,以便能够按预期方式获得结果,返回的对象还必须包含“ product”属性,否则不会返回任何内容(例如,使用JSON REST服务)
public class ComponentExpanded implements Serializable {
private String product;
....
与
AggregationResults<ComponentExpanded> results =
mongoTemplate.aggregate(agg,mongoTemplate.getCollectionName(Component.class), ComponentExpanded.class);
问题出在您所观察到的productId
和之间的类型不匹配_id
。
要加入此类数据,我们需要执行不相关的子查询,并且并非每个“新”功能都会使其立即进入抽象层,例如spring-mongo
。
尝试这个:
Aggregation agg = Aggregation.newAggregation(l -> new Document("$lookup",
new Document("from", mongoTemplate.getCollectionName(Product.class))
.append("let", new Document("productId", new Document("$toObjectId", "$productId")))
.append("pipeline",
Arrays.asList(new Document("$match",
new Document("$expr",
new Document("$eq", Arrays.asList("$_id", "$$productId"))))))
.append("as", "product")),
Aggregation.unwind("product", Boolean.TRUE));
AggregationResults<Component> results = mongoTemplate.aggregate(agg,
mongoTemplate.getCollectionName(Component.class), Component.class);
return results.getMappedResults();
MongoPlayground在这里检查外壳查询的外观。
注意:对于Java v1.7
,您需要实现AggregationOperation
如下所示:
AggregationOperation l = new AggregationOperation() {
@Override
public Document toDocument(AggregationOperationContext context) {
return new Document(...); // put here $lookup stage
}
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句