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

前端之家收集整理的这篇文章主要介绍了java – spring-data-mongodb:findAll(),包含输入文档列表和嵌入式DBRef文档的搜索参数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

我的模型如下:

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

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

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

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

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

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

我的DAO如下:

@H_404_10@public interface CarDAO extends MongoRepository

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

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

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

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

我收到此错误

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

怎么样?

我需要做两个DAO呼叫吗?

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

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

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

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

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

谢谢

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

将UsedIn类更新为以下.

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

Mongo Shell查询

@H_404_10@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聚合代码

@H_404_10@ 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

加载数据的方法.

汽车数据

@H_404_10@public void saveCar() { carDao.deleteAll(); CarDocument carDocument1 = new CarDocument(); carDocument1.setId(1L); carDocument1.setName("audi"); carDocument1.setMadeInCountry("germany"); carDao.save(carDocument1); }

工具数据

@H_404_10@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 ?

@H_404_10@ public List

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

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

@H_404_10@List

猜你在找的Spring相关文章