普遍使用的{from, localField, foreignField, as}形式的
$lookup
只实现了单条件连接表,但是有时候我们会碰到需要多条件连接的情况,其实mongodb在3.6
版本之后已经支持了这种操作(详情见这里),但是SpringDataMongo的MongoOperation(MongoTemplate)还没有去适配。这里提供了一个简单的工具类
LookupLetPipelinesOperation.java
package vip.starhouse.propertyservicemp.infrastructure.common.db.mongo.custom;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperationContext;
import org.bson.Document;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @author 金艺群
* @description
* @date 2021/07/28
**/
public class LookupLetPipelinesOperation implements AggregationOperation {
private String jsonOperation;
private String from;
private String as;
private List<String> foreignFields;
private List<String> localFields;
public LookupLetPipelinesOperation(String jsonOperation) {
this.jsonOperation = jsonOperation;
}
public LookupLetPipelinesOperation() {
}
@Override
public Document toDocument(AggregationOperationContext aggregationOperationContext) {
return aggregationOperationContext.getMappedObject(Document.parse(jsonOperation));
}
public static LookupLetPipelinesOperation Look