MongoDB - 如何检查数组中的某些元素是否满足数组中另一个元素的条件?



我的Java APP中有以下对象:

[
{
"from" : "11/12/2020",
"to" : "12/12/2019"
},
{
"from" : "13/12/2020",
"to" : "14/12/2020
}
]

在我的数据库中,我有以下对象:

[
{
"from" : "01/12/2020",
"to" : "14/12/2019"
},
{
"from" : "20/12/2020",
"to" : "30/12/2020
}
]

我可以编写什么查询来检查我的第一个 Java APP 中任何对象fromto的任何日期是否比数据库对象中的任何fromto值为 gte 或 lte?

谢谢!

您必须遍历所有数据库条目并将它们与您的对象进行比较。 如果您使用 JPA 存储库与数据库进行通信,则可以这样做:

List<Entry> matches = dataRepository.findAll().stream().filter(dbEntry -> dbEntry.equals(myEntry)).collect(Collectors.asList());

ArrayList 现在将包含数据库中的所有匹配项。

最新更新