java - 我需要使用过滤的数组项来检索 MongoDB 的对象
问题描述
我只需要使用两个日期来检索我的 MongoDB 集合中的所有文档,以及数组中的过滤项。
这是我的 2 个文档的示例;
{
"_id" : ObjectId("5f18fa823406b7000132d097"),
"last_date" : "22/07/2020 23:48:32",
"history_dates" : [
"22/07/2020 23:48:32",
"22/07/2020 00:18:53",
"23/07/2020 00:49:12",
"23/07/2020 01:19:30"
],
"hostname" : "MyHostname1",
"ip" : "142.0.111.79",
"component" : "C:\\Windows\\System32\\es-ES\\KernelBase.dll.mui",
"process" : "LogonUI.exe",
"date" : "23/07/2020 10:26:04",
}
{
"_id" : ObjectId("5f18fa823406b7000132d098"),
"last_date" : "22/07/2020 23:48:33",
"history_dates" : [
"22/07/2020 23:48:33",
"23/07/2020 00:18:53",
],
"hostname" : "MyHostName2",
"ip" : "142.0.111.54",
"component" : "C:\\Windows\\System32\\es-ES\\KernelBase.dll.mui",
"process" : "svchost.exe",
"date" : "23/07/2020 10:26:04",
}
我需要查找我的数据库(使用 Spring Data),以检索相同的对象,但在收到的 2 个日期之间过滤了“history_dates”数组。
例如,如果我收到的 2 个日期是:“23/07/2020”和“24/07/2020”,我希望 MongoDB 返回下一个对象;
{
"_id" : ObjectId("5f18fa823406b7000132d097"),
"last_date" : "22/07/2020 23:48:32",
"history_dates" : [
"23/07/2020 00:49:12",
"23/07/2020 01:19:30"
],
"hostname" : "MyHostname1",
"ip" : "142.0.111.79",
"component" : "C:\\Windows\\System32\\es-ES\\KernelBase.dll.mui",
"process" : "LogonUI.exe",
"date" : "23/07/2020 10:26:04",
}
{
"_id" : ObjectId("5f18fa823406b7000132d098"),
"last_date" : "22/07/2020 23:48:33",
"history_dates" : [
"23/07/2020 00:18:53"
],
"hostname" : "MyHostName2",
"ip" : "142.0.111.54",
"component" : "C:\\Windows\\System32\\es-ES\\KernelBase.dll.mui",
"process" : "svchost.exe",
"date" : "23/07/2020 10:26:04",
}
我真的对 MongoDB 的查询一无所知,我整个星期都在尝试用 Spring Data 来做这个。
更新 1。
谢谢 varman,你知道我怎样才能检索过滤数组不为空的文档吗?
解决方案
所以基本上你需要做过滤。MongoTemplate
为mongodb提供了很多操作,如果某些方法在MongoTemplate中不存在,我们可以使用BsonDocument
模式。在这种情况下,试试这篇文章:Trick to covert mongo shell query。
实际上,您需要一个 Mongo 查询,如下所示。使用$addFields
下面显示的方法之一。但是你可以使用$project
,$set
等等。这里会$addFields
覆盖你的history_dates
. (它也用于向文档添加新字段)。
{
$addFields: {
history_dates: {
$filter: {
input: "$history_dates",
cond: {
$and: [{
$gt: ["$$this", "23/07/2020"]
},
{
$lt: ["$$this", "24/07/2020"]
}
]
}
}
}
}
}
工作Mongo 游乐场。
您需要将其转换为弹簧数据。所以@Autowired
你班上的MongoTemplate。
@Autowired
MongoTemplate mongoTemplate;
方法是,
public List<Object> filterDates(){
Aggregation aggregation = Aggregation.newAggregation(
a->new Document("$addFields",
new Document("history_dates",
new Document("$filter",
new Document("input","$history_dates")
.append("cond",
new Document("$and",
Arrays.asList(
new Document("$gt",Arrays.asList("$$this","23/07/2020")),
new Document("$lt",Arrays.asList("$$this","24/07/2020"))
)
)
)
)
)
)
).withOptions(AggregationOptions.builder().allowDiskUse(Boolean.TRUE).build());
return mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(YOUR_CLASS.class), Object.class).getMappedResults();
}
Mongo 模板不提供$addFields
和的添加方法$filter
。所以我们只使用 bson 文档模式。我还没有在春天测试过这个。
推荐阅读
- java - java.lang.IllegalArgumentException:不是带有 @Entity 和 @Repository 的托管类型 使用 Spring Boot 2 设置
- python - 两个字符串数组的乘积
- assembly - 操作码到字节的主要图表
- android - java.lang.IllegalStateException:没有maybeRun就不能取消调用
- python - 解码法语口音不适用于 utf-8
- javascript - 从承诺中获取数据
- node.js - 我收到错误:ms 团队中的机器人已禁用租户管理员
- javascript - 找出两个数组之间的差异(Javascript 算法)+ 扩展运算符的时髦使用
- android - 错误无法启动模拟器。原因:没有找到模拟器作为 `emulator -list-avds` 的输出。在 Windows 10 中
- python - 自我价值与自我