Mongodb $查找Spring数据mongo

Anh*_*Bui 6 java mongodb spring-mongo

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

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

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

我在Google上发现但尚无答案.

小智 8

使用Spring Data MongoDB加入两个集合

员工类

__CODE__

系类

__CODE__

员工结果类`public class EmpDeptResult {

class Employee {
    private String _id;
    private String name;
    private String dept_id;
}
Run Code Online (Sandbox Code Playgroud)

}`

EmployeeSerivce`public class EmployeeService {

class Department {
    private String _id;
    private String dept_name;
}
Run Code Online (Sandbox Code Playgroud)

}`

  • 我想加入多个领域,有什么办法吗? (2认同)

Bla*_*ven 7

并非每个"新"特征都会立即进入诸如类的抽象层.

所以你需要做的就是定义一个使用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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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")
        )
    )
)
Run Code Online (Sandbox Code Playgroud)

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

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


VK3*_*321 7

下面是一个例子:

收藏

{
"_id" : ObjectId("5a198074ed31adaf5d79fe8a"),
"title" : "Post 1",
"authors" : [1, 2]
},
{
"_id" : ObjectId("5a198074ed31adaf5d79fe8d"),
"title" : "Post 2",
"authors" : [2]
}
Run Code Online (Sandbox Code Playgroud)

收藏用户

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

具有查找和匹配功能的 Mongodb 查询

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

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<BasicDBObject> results = mongoOperation.aggregate(aggregation, "users", BasicDBObject.class).getMappedResults();
Run Code Online (Sandbox Code Playgroud)


Viv*_*ngh 7

回答这个问题为时已晚,但它可能会帮助其他面临相同问题的人。如果您使用的是 spring-boot-data-mongodb-2.0 或更高版本,那么有一种简单的方法可以实现这一点。

AggregationOperation match = Aggregation.match(Criteria.where("username").is("user001")));
AggregationOperation query = Aggregation.lookup("NewfeedContent", "content.contentId", "_id", "NewfeedContent");
// If you want to unwind
//AggregationOperation unwind = Aggregation.unwind("Patient");
Aggregation agr = Aggregation.newAggregation(query, match, unwind);
AggregationResults<Document> result = springTemplate.aggregate(agr, "CollectionName", Document.class);
Run Code Online (Sandbox Code Playgroud)