Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我需要检索MongoDB';仅使用过滤的';s数组项_Java_Spring_Mongodb - Fatal编程技术网

Java 我需要检索MongoDB';仅使用过滤的';s数组项

Java 我需要检索MongoDB';仅使用过滤的';s数组项,java,spring,mongodb,Java,Spring,Mongodb,我只需要检索两个日期,MongoDB集合中的所有文档,以及数组中的过滤项 这是我的两份文件的一个例子 { "_id" : ObjectId("5f18fa823406b7000132d097"), "last_date" : "22/07/2020 23:48:32", "history_dates" : [ &quo

我只需要检索两个日期,MongoDB集合中的所有文档,以及数组中的过滤项

这是我的两份文件的一个例子

{
        "_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数据),以检索相同的对象,但“history_dates”的数组在收到的两个日期之间进行过滤

例如,如果我的两个接收日期是:“2020年7月23日”和“2020年7月24日”,我希望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数据来实现这一点

更新1


谢谢varman,你知道我怎样才能检索到过滤数组不为空的文档吗?

所以基本上你需要进行过滤
MongoTemplate
为mongodb提供了大量的操作,如果MongoTemplate中没有一些方法,我们可以使用Bson
Document
模式。在这种情况下,请尝试以下文章:

实际上,您需要一个Mongo查询,如下所示。使用下面显示的方法之一
$addFields
。但您可以使用
$project
$set
等。此处
$addFields
覆盖您的
历史日期。(它还用于向文档中添加新字段)

工作

您需要将其转换为spring数据。因此,
@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();
}
公共列表筛选器日期(){
Aggregation=Aggregation.newAggregation(
a->新文档(“$addFields”,
新文件(“历史记录和日期”,
新文档(“$filter”,
新文档(“输入”,“历史记录\日期”)
.append(“cond”,
新文件(“$and”,
Arrays.asList(
新文件(“$gt”,Arrays.asList(“$$this”,“23/07/2020”)),
新文档(“$lt”,Arrays.asList(“$$this”,“24/07/2020”))
)
)
)
)
)       
)
).withOptions(AggregationOptions.builder().allowDiskUse(Boolean.TRUE.build());
返回mongoTemplate.aggregate(聚合,mongoTemplate.getCollectionName(您的_CLASS.CLASS),Object.CLASS).getMappedResults();
}

Mongo模板不为
$addFields
$filter
提供添加方法。所以我们只使用bson文档模式。我还没有在春季测试过这个功能。

你有实现过的代码吗?我一直在尝试使用MongoCollection,但我无法让它正常工作,我真的很茫然是的!!!!非常感谢您的代码片段和解释!你知道有没有办法不使用空的过滤数组检索对象?是的,有很多方法,如果问题与此相关,只需使用诸如“更新1”之类的指标更新问题,否则打开另一个问题并在此处添加url。哈哈,只需发布一个示例,我不理解你提到的“过滤数组不为空”是否需要省略历史日期为空的文档?请使用
$match
。在java中播放grpund和,
匹配(Criterial.where(“history_dates”).ne(Arrays.asList())
$$addFields
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();
}