微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – spring-data-mongodb:findAll(),包含输入文档列表和嵌入式DBRef文档的搜索参数

我使用spring-data-mongo并尝试使用params访问dbref对象.
我的项目看起来像这样:

我的模型如下:

一世.第一份文件是“汽车”

@Document("cars")
class CarDocument {
   @Id
   private String id;
   private String name;
   private String madeInCountry;
   private String model;
   private String madeInYear;
}

II.第二个文件是“工具”

Document("tools")
class ToolDocument {
   @Id
   private String id;
   private String name;
   private String madeInCountry;
   private String madeInYear;
   private List

III.第三个是嵌入式模型“UsedIn”(ii.)
第三个嵌入式模型表示在制造厂中用于制造汽车的工具的位置.

class UsedIn {
   @DBRef
   private CarDocument car;
   private DateTime usedDate;
   private String usedByUsername;
}

我的DAO如下:

public interface CarDAO extends MongoRepository

现在我需要列出特定汽车中使用的所有“工具”.

一个.汽车制造时在国内:“德国”和
湾工具制造国家:“德国”

我看到我们无法直接在DBRef文档上应用搜索.
喜欢 :

String madeInCountry = "germany";
toolDAO.findByMadeInCountryAndUsedInCarMadeInCountry(madeInCountry,madeInCountry);

我收到此错误

"Invalid path reference car.madeInCountry! Associations can only be pointed to directly or via their id property!"

怎么样?

我需要做两个DAO呼叫吗?

一世.首先让所有带有madeInCountry的汽车都是德国的

String madeInCountry = "germany";
carDAO.findByMadeInCountry(madeInCountry);

II. findTools由carDocuments和String列表组成.

我不知道,如何用CarDocuments和madeInCountry字符串列表调用这个dao?

我需要使用一些$lookup功能吗?

谢谢

最佳答案
您可以尝试以下聚合.

将UsedIn类更新为以下.

 private Long carId;
 private CarDocument car;
 private Date usedDate;
 private String usedByUsername;

Mongo Shell查询

db.tools.aggregate([{
    "$match": {
        "madeInCountry": "germany"
    }
},{
    "$unwind": "$usedIn"
},{
    "$lookup": {
        "from": "cars","localField": "usedIn.carId","foreignField": "_id","as": "usedIn.car"
    }
},{
    "$unwind": "$usedIn.car"
},{
    "$match": {
        "usedIn.car.madeInCountry": "germany"
    }
},{
    "$group": {
        _id: "$_id",usedIns: {
            "$push": "$usedIn"
        }
    }
}])

Spring聚合代码

 Criteria toolQuery = Criteria.where("madeInCountry").in("germany");
 MatchOperation toolMatchOperation = new MatchOperation(toolQuery);
 LookupOperation lookupOperation = LookupOperation.newLookup().
                from("cars").
                localField("usedIn.carId").
                foreignField("_id").
                as("usedIn.car");
 Criteria carQuery = Criteria.where("usedIn.car.madeInCountry").is("germany");
 MatchOperation carMatchOperation = new MatchOperation(carQuery);

 TypedAggregation

加载数据的方法.

汽车数据

public void saveCar() {
    carDao.deleteall();

    CarDocument carDocument1 = new CarDocument();
    carDocument1.setId(1L);
    carDocument1.setName("audi");
    carDocument1.setMadeInCountry("germany");

    carDao.save(carDocument1);
}

工具数据

public void savetool() {

    toolDao.deleteall();

    ToolDocument toolDocument1 = new ToolDocument();
    toolDocument1.setId(1L);
    toolDocument1.setName("Wrench");
    toolDocument1.setMadeInCountry("germany");

    UsedIn usedIn1 = new UsedIn();
    usedIn1.setCarId(1L);
    usedIn1.setUsedByUsername("user");
    usedIn1.setUsedDate(new Date());

    List

更新:

回答有关访问DBRef的问题

ii. findTools by the list of carDocuments and String.

I dont kNow,how to call this dao with list of CarDocuments and
madeInCountry String ?

 public List

就像我在第一个评论中提到的那样,您需要的第二个调用是对嵌入式dbref的调用以及汽车文档列表值.查询将查找匹配项,并在找到工具文档的匹配项时返回所有汽车文档.这意味着您将获得德国制造的工具文件,其中至少使用了德国制造的文件.

分片更新($lookup equalivent)(从这里获取的想法MongoDB to Use Sharding with $lookup Aggregation Operator)

List

原文地址:https://www.jb51.cc/spring/431929.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐