0

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

 {
     "_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 之间”的子文档。

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

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 这样的东西。有解决方法吗?

无论如何,$filter 的效率如何?你能想出一种更有效/更实用的方式在 mongodb 中构建这种数据吗?

提前致谢!

4

3 回答 3

0

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

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();
于 2017-01-04T15:34:29.813 回答
0

Spring Data MongoDB 1.10 RC1 (Ingalls), 2.0 M2 (Kay)版本(在撰写本文时)增加了对的支持,$filter可以按如下方式实现:

Aggregation.newAggregation(Entity.class,
    match(Criteria.where("_id").is(1)),
    project("sensorName", "samplePeriod")
        .and(
            filter("data")
                .as("item")
                .by(
                    GTE.of(field("item.timestamp"), 1483537204000)
                    .LTE.of(field("item.timestamp"), 1483537214000)
                )
        ).as("data")
    )
于 2017-01-04T16:05:09.377 回答
0

我认为同样可以通过使用 unwind 和额外的匹配来实现。Spring mongo 驱动程序确实提供了对 unwind 的支持,并且看起来更简洁。

aggregate([{
 $match: {
    "_id": 1
   }
 }, {
  $unwind : "$data"
 },{
   $match : {'data.timestamp' : {$gte : 1483537204000, $lte : 1483537214000}}
 }, {
  $group : {
      _id : $_id,
      data : {$push:$data}
  }
 }])
于 2017-01-04T15:42:55.810 回答