我只需要使用过滤的数组项来检索MongoDB的对象 [英] I need to retrieve MongoDB's object just with filtered's array item
问题描述
我只需要检索两个日期,即MongoDB集合中的所有文档,以及数组中已过滤的项目.
I'm needing to retrieve just with two dates, all the documents from my MongoDB's collection, with the filtered items from the array.
这是我的两个文档的示例;
This is an example of 2 of my documents;
{
"_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),以检索相同的对象,但是要在收到的两个日期之间过滤"history_dates"数组.
I'm needing to make a find to my database (Using Spring Data), to retrieve the same objects, but with the "history_dates"'s array filtered between the 2 dates recieved.
例如,如果我收到的2个日期是:"23/07/2020",和"24/07/2020",我希望MongoDB返回下一个对象;
For example, if my 2 recieved dates are: "23/07/2020" and "24/07/2020", I want MongoDB to return the next objects;
{
"_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整个星期.
I'm really ignorant about MongoDB's queries, and I have been trying to make this with Spring Data all the week.
更新1.
感谢varman,您知道我如何才能检索过滤后的数组不为空的文档吗?
Thanks varman, and do you know how can i just retrieve the documents with filtered arrays not empty?
推荐答案
因此,基本上,您需要进行过滤. MongoTemplate
为mongodb提供了很多操作,如果MongoTemplate中不存在某些方法,我们可以使用Bson Document
模式.在这种情况下,请尝试以下文章:隐蔽mongo shell的技巧查询.
So basically you need to do filter. MongoTemplate
offers a lot of operation for mongodb, if some methods don't exist in MongoTemplate, we can go with Bson Document
pattern. In that case, try this article: Trick to covert mongo shell query.
实际上,您需要像以下这样的Mongo查询.使用 $ addFields
如下所示的方法之一.但是您可以使用 $ project
, $ set
等.在这里, $ addFields
会覆盖您的 history_dates
.(它也用于向文档添加新字段.)
Actually you need a Mongo query something like following. Using $addFields
one of the methods shown below. But you can use $project
, $set
etc. Here $addFields
overwrites your history_dates
. (It uses to add new fields to document too).
{
$addFields: {
history_dates: {
$filter: {
input: "$history_dates",
cond: {
$and: [{
$gt: ["$$this", "23/07/2020"]
},
{
$lt: ["$$this", "24/07/2020"]
}
]
}
}
}
}
}
工作蒙戈游乐场.
您需要将其转换为spring数据.因此,您班上的 @Autowired
MongoTemplate.
You need to convert this into spring data. So @Autowired
the MongoTemplate in you class.
@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文档模式.我还没有在春季测试过.
Mongo template doesn't provide add methods for $addFields
and $filter
. So we just go with bson document pattern. I haven't tested this in Spring.
这篇关于我只需要使用过滤的数组项来检索MongoDB的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!