如何编写复杂文档结构的MongoTemplate查询和条件

如何编写复杂文档结构的MongoTemplate查询和条件

本文介绍了如何编写复杂文档结构的MongoTemplate查询和条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MongoTemplate的新手.我想定义一个org.springframework.data.mongodb.core.query.Query和org.springframework.data.mongodb.core.query.Criteria来从具有复杂文档结构的集合中获取数据.这是文件

I am new to MongoTemplate. I want to define a org.springframework.data.mongodb.core.query.Query and org.springframework.data.mongodb.core.query.Criteria to fetch the data from a collection which has a complex document structure.This is the document

{
"_id" : {
    "SId" : "ANBS",
    "AssetId" : "ANBS_BS21",
    "ST" : NumberLong(1479114000) //StartDate
},
"ET" : NumberLong(1479117599) //EndDate,
"TS" : [
    NumberLong(1479114000),
    NumberLong(1479114600),
    NumberLong(1479115200),
    NumberLong(1479115800),
    NumberLong(1479116400),
    NumberLong(1479117000)
],
"Tags" : {
    "ActivePower" : {
        "Avg" : [
            16427.575,
            16991.01,
            16708.2016666667,
            16488.335,
            17230.1933333333,
            15996.9783333333
        ]
    },
    "WindSpeed" : {
        "Avg" : [
            64.4266666666667,
            60.8583333333333,
            65.275,
            62.8766666666667,
            63.2166666666667,
            63.14
        ]
    }
}

我想获取给定TimeRange(StartTime和EndTime)的所有文档,WHERE AssetId =? AND startTime> = ST AND endTime< = ET.

有人可以帮助我解决这个问题吗?我不想要mongo查询,但是我想要基于MongoTemplate的查询和条件.预先感谢.

Can anybody help me with this problem? I dont want the mongo query, but I want the MongoTemplate based query and criteria. Thanks in advance.

推荐答案

我得到了一个解决方案,它非常简单.发布此答案,以便对他人有所帮助.

I got a solution, it was very simple. Posting this answer so that it can help someone else.

Query query = new Query(Criteria
            .where("_id.SId").is(siteId)
            .and("_id.AssetId").is(assetId)
            .and("_id.ST").gte(startTimeRange)
            .and("ET").lte(endTimeRange));
return mongoTemplate.find(query, MyCollection.class, "mycollection");

这篇关于如何编写复杂文档结构的MongoTemplate查询和条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 10:12