Spring数据mongo中的Mongodb $ lookup

我是一个新的Mongodb,我在使用java spring查询时遇到了问题。

我想在Spring数据中使用这个shell

db.NewFeed.aggregate([ { $match : {username : "user001"} }, { $lookup: { from: "NewfeedContent", localField: "content.contentId", foreignField: "_id", as: "NewfeedContent" } } ]) 

我在Google上找到了,但还没有答案。

并非每个“新”特征都会立即进入诸如spring-mongo之类的抽象层。

因此,您需要做的就是定义一个使用AggregationOperation接口的类,该接口将直接指定BSON对象作为其内容:

 public class CustomAggregationOperation implements AggregationOperation { private DBObject operation; public CustomAggregationOperation (DBObject operation) { this.operation = operation; } @Override public DBObject toDBObject(AggregationOperationContext context) { return context.getMappedObject(operation); } } 

然后你可以在你的聚合中使用这样的:

 Aggregation aggregation = newAggregation( match( Criteria.where("username").is("user001") ), new CustomAggregationOperation( new BasicDBObject( "$lookup", new BasicDBObject("from", "NewFeedContent") .append("localField","content.contentId") .append("foreignField", "_id") .append("as", "NewFeedContent") ) ) ) 

其中显示了自定义类与内置的match()管道帮助器混合。

在每个帮助器下面发生的所有事情是它们序列化为BSON表示,例如使用DBObject 。 所以这里的构造函数直接获取对象,并直接从.toDBObject()返回它,这是在序列化pipline内容时将被调用的接口上的标准方法。

使用Spring Data MongoDB加入两个集合

员工类

class Employee { private String _id;enter code here private String name; private String dept_id; }

系类

class Department { private String _id; private String dept_name; }

员工结果类`public class EmpDeptResult {

 private String _id; private String name; private List departments; 

}`

EmployeeSerivce`public class EmployeeService {

 @Autowired private MongoTemplate mongoTemplate; private Logger LOGGER = LoggerFactory.getLogger(EmployeeService.class); public void lookupOperation(){ LookupOperation lookupOperation = LookupOperation.newLookup() .from("Department") .localField("dept_id") .foreignField("_id") .as("departments"); Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(Criteria.where("_id").is("1")) , lookupOperation); List results = mongoTemplate.aggregate(aggregation, "Employee", EmpDeptResult.class).getMappedResults(); LOGGER.info("Obj Size " +results.size()); } 

}`

这是一个例子:

收集post

 { "_id" : ObjectId("5a198074ed31adaf5d79fe8a"), "title" : "Post 1", "authors" : [1, 2] }, { "_id" : ObjectId("5a198074ed31adaf5d79fe8d"), "title" : "Post 2", "authors" : [2] } 

收集用户

 { "_id" : ObjectId("5a18b483ed31ada08fd6ed82"), "userId" : 1, "name" : "Vinod Kumar" }, { "_id" : ObjectId("5a18b483ed31ada08fd6ed83"), "userId" : 2, "name" : "Jim Hazel" }, { "_id" : ObjectId("5a18b483ed31ada08fd6ed84"), "userId" : 3, "name" : "Alex Wong" } 

具有查找和匹配的Mongodb查询

 db.users.aggregate([ { $lookup: { from: "users", localField: "userid", foreignField: "authors", as: "post" } }, { $match: { "post": { $ne: [] } } } ]).pretty() 

Spring Mongoopration语法

 LookupOperation lookupOperation = LookupOperation.newLookup(). from("posts"). localField("userid"). foreignField("authors"). as("post"); AggregationOperation match = Aggregation.match(Criteria.where("post").size(1)); Aggregation aggregation = Aggregation.newAggregation(lookupOperation, match); List results = mongoOperation.aggregate(aggregation, "users", BasicDBObject.class).getMappedResults();