1. <i id='igApb'><tr id='igApb'><dt id='igApb'><q id='igApb'><span id='igApb'><b id='igApb'><form id='igApb'><ins id='igApb'></ins><ul id='igApb'></ul><sub id='igApb'></sub></form><legend id='igApb'></legend><bdo id='igApb'><pre id='igApb'><center id='igApb'></center></pre></bdo></b><th id='igApb'></th></span></q></dt></tr></i><div id='igApb'><tfoot id='igApb'></tfoot><dl id='igApb'><fieldset id='igApb'></fieldset></dl></div>

    <tfoot id='igApb'></tfoot>

    1. <small id='igApb'></small><noframes id='igApb'>

        <bdo id='igApb'></bdo><ul id='igApb'></ul>
      <legend id='igApb'><style id='igApb'><dir id='igApb'><q id='igApb'></q></dir></style></legend>

      在mongodb中查询一个文档及其所有匹配条件的子文档(使用spring)

      Query a document and all of its subdocuments that match a condition in mongodb (using spring)(在mongodb中查询一个文档及其所有匹配条件的子文档(使用spring))
        <bdo id='bDD2b'></bdo><ul id='bDD2b'></ul>

        • <small id='bDD2b'></small><noframes id='bDD2b'>

        • <i id='bDD2b'><tr id='bDD2b'><dt id='bDD2b'><q id='bDD2b'><span id='bDD2b'><b id='bDD2b'><form id='bDD2b'><ins id='bDD2b'></ins><ul id='bDD2b'></ul><sub id='bDD2b'></sub></form><legend id='bDD2b'></legend><bdo id='bDD2b'><pre id='bDD2b'><center id='bDD2b'></center></pre></bdo></b><th id='bDD2b'></th></span></q></dt></tr></i><div id='bDD2b'><tfoot id='bDD2b'></tfoot><dl id='bDD2b'><fieldset id='bDD2b'></fieldset></dl></div>

          1. <tfoot id='bDD2b'></tfoot>
              <tbody id='bDD2b'></tbody>
              <legend id='bDD2b'><style id='bDD2b'><dir id='bDD2b'><q id='bDD2b'></q></dir></style></legend>
                本文介绍了在mongodb中查询一个文档及其所有匹配条件的子文档(使用spring)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个 MongoDB 存储来自不同传感器的数据.它的结构如下:

                I have a MongoDB storing data from different sensors. It has the following structure:

                 {
                     "_id" : 1,
                     "sensorName" : "Heart Rate",
                     "samplePeriod" : 1000,
                     "data" : [
                             {
                                 "timestamp" : NumberLong("1483537204046"),
                                 "dataPoints" : [ 68 70 ]
                             },
                             {
                                 "timestamp" : NumberLong("1483537206046"),
                                 "dataPoints" : [ 68 70 ]
                             }
                     ]
                }
                {
                    "_id" : 2,
                    "sensorName" : "Ambient Light",
                    "samplePeriod" : 500,
                    "data" : [
                            {
                                "timestamp" : NumberLong("1483537204058"),
                                "dataPoints" : [ 56, 54, 54, 54 ]
                            },
                            {
                                "timestamp" : NumberLong("1483537206058"),
                                "dataPoints" : [ 56, 54, 54, 54 ]
                            }
                    ]
                }
                

                例如,现在我需要心率" - 包含所有字段及其数据"字段的文档 - 匹配条件时间戳在 1483537204000 和 1483537214000 之间"的子文档.

                Now for example i need the "Heart Rate" - document with all of its fields and those of its "data" - subdocuments matching the condition "timestamp between 1483537204000 and 1483537214000".

                我已经在另一个问题中得到了关于如何在 mongo shell 中执行此操作的答案.请参阅此代码:

                I already got the answer on how to do this in the mongo shell in another Question. See this code:

                aggregate([{
                    $match: {
                        "_id": 1
                    }
                }, {
                    "$project": {
                        "_id": 1,
                        "sensorName": 1,
                        "samplePeriod": 1,
                        "data": {
                            "$filter": {
                                "input": "$data",
                                "as": "result",
                                "cond": {
                                    $and: [{
                                        $gte: ["$$result.timestamp", 1483537204000]
                                    }, {
                                        $lte: ["$$result.timestamp", 1483537214000]
                                    }]
                                }
                            }
                        }
                    }
                }])
                

                但是我如何在 java spring-data 中做到这一点?spring-data 中似乎没有像 $filter 这样的东西.有解决办法吗?

                But how do I do this in java spring-data? It seems there is nothing like $filter in spring-data. Is there a workaround?

                $filter 的效率如何?您能想出一种更有效/更实用的方式在 mongodb 中构建此类数据吗?

                How efficient is $filter anyway? Can you think of a more efficient/practical way of structuring this kind of data in mongodb?

                提前致谢!

                推荐答案

                你需要使用spring mongo数据依赖中提供的MongoTemplate.当前发行版本中没有对 $filter 的开箱即用支持.使用 AggressionExpression.在项目中包括以下投影.使用 1.8.5 spring mongo 数据版本.

                You'll need to make use of MongoTemplate provided in the spring mongo data dependency. There is no out of box support for $filter in the current release version. Make use of AggressionExpression. Include below projection in project. Use 1.8.5 spring mongo data version.

                Aggregation aggregation = newAggregation(
                        match(Criteria.where("_id").is(1)),
                        project( "_id", "sensorName", "samplePeriod").and(new AggregationExpression() {
                            @Override
                            public DBObject toDbObject(AggregationOperationContext aggregationOperationContext) {
                                DBObject filter = new BasicDBObject("input", "$data").append("as", "result").append("cond",
                                        new BasicDBObject("$and", Arrays.<Object> asList(new BasicDBObject("$gte", Arrays.<Object> asList("$$result.timestamp", 1483537204000L)),
                                                new BasicDBObject("$lte", Arrays.<Object> asList("$$result.timestamp", 1483537214000L)))));
                                return new BasicDBObject("$filter", filter);
                            }
                        }).as("data")
                );
                
                List<BasicDBObject> dbObjects = monoTemplate.aggregate(aggregation, "collectionname", BasicDBObject.class).getMappedResults();
                

                这篇关于在mongodb中查询一个文档及其所有匹配条件的子文档(使用spring)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)
                1. <tfoot id='ITgAA'></tfoot>

                  <small id='ITgAA'></small><noframes id='ITgAA'>

                2. <i id='ITgAA'><tr id='ITgAA'><dt id='ITgAA'><q id='ITgAA'><span id='ITgAA'><b id='ITgAA'><form id='ITgAA'><ins id='ITgAA'></ins><ul id='ITgAA'></ul><sub id='ITgAA'></sub></form><legend id='ITgAA'></legend><bdo id='ITgAA'><pre id='ITgAA'><center id='ITgAA'></center></pre></bdo></b><th id='ITgAA'></th></span></q></dt></tr></i><div id='ITgAA'><tfoot id='ITgAA'></tfoot><dl id='ITgAA'><fieldset id='ITgAA'></fieldset></dl></div>

                        <tbody id='ITgAA'></tbody>
                    • <legend id='ITgAA'><style id='ITgAA'><dir id='ITgAA'><q id='ITgAA'></q></dir></style></legend>

                      • <bdo id='ITgAA'></bdo><ul id='ITgAA'></ul>