使用 Casbah [Scala] 查询深层嵌套 MongoDB 对象
Query Deep Nested MongoDB Object with Casbah [Scala]
{'_id': ObjectId('589df735be9fa2077dc5bb2e'),
'name': 'train.2',
'samples': [{'processed': {'eulerAngles': [1.946705159018607,
-0.01405100477972897,
0.02015610032297892]},
'raw': {'accel': [0.01548936311155558,
0.0002011665492318571,
0.02126962691545486],
'gyro': [0.06626824289560318,
0.1328225582838058,
0.001385239884257317]},
'timestamp': '2017-02-10T17:24:04.283Z'},
{'processed': {'eulerAngles': [1.948553665755181,
-0.01403613777322358,
0.01932380767991558]},
'raw': {'accel': [0.01380419824272394,
-0.001462434651330113,
0.01273023523390293],
'gyro': [0.09209229052066803,
0.07342914491891861,
0.01548820454627275]},
'timestamp': '2017-02-10T17:24:04.293Z'}}]}
对于样本数组中的每个样本,我都需要提取数组加速度。我写了下面的代码
val el = db("samples").findOne(MongoDBObject("name" -> name))
var t_series_seq: Array[Array[Double]] = Array()
for (m <- el.get("samples")) t_series_seq :+ m("raw")("accel")
这是返回 value foreach is not a member of AnyRef
。我需要告诉 el.get("samples") 类型,但我不知道如何告诉它是一个复杂的 Map 对象。
好吧,Casbah 文档真的很糟糕。我实际上是通过阅读专门在 Scala for Data Science 中使用 MongoDB with Scala 的章节来了解这一点的。
我的代码如下:
val dbSamples = db("samples").find(MongoDBObject("name" -> name))
var timesSeries: Array[TimeSeries] = Array()
for (s <- dbSamples) {
var tSeries_seq: Array[Array[Double]] = Array()
val measures = s.getAs[List[DBObject]]("samples").get
for (m <- measures) {
println(m.getAs[DBObject]("raw").get.getAs[List[Double]]("accel").get.toArray)
tSeries_seq = tSeries_seq :+ m.getAs[DBObject]("raw").get.getAs[List[Double]]("accel").get.toArray
}
timesSeries = timesSeries :+ new TimeSeries(tSeries_seq, s.getAs[Integer]("valid").get)
}
timesSeries
{'_id': ObjectId('589df735be9fa2077dc5bb2e'),
'name': 'train.2',
'samples': [{'processed': {'eulerAngles': [1.946705159018607,
-0.01405100477972897,
0.02015610032297892]},
'raw': {'accel': [0.01548936311155558,
0.0002011665492318571,
0.02126962691545486],
'gyro': [0.06626824289560318,
0.1328225582838058,
0.001385239884257317]},
'timestamp': '2017-02-10T17:24:04.283Z'},
{'processed': {'eulerAngles': [1.948553665755181,
-0.01403613777322358,
0.01932380767991558]},
'raw': {'accel': [0.01380419824272394,
-0.001462434651330113,
0.01273023523390293],
'gyro': [0.09209229052066803,
0.07342914491891861,
0.01548820454627275]},
'timestamp': '2017-02-10T17:24:04.293Z'}}]}
对于样本数组中的每个样本,我都需要提取数组加速度。我写了下面的代码
val el = db("samples").findOne(MongoDBObject("name" -> name))
var t_series_seq: Array[Array[Double]] = Array()
for (m <- el.get("samples")) t_series_seq :+ m("raw")("accel")
这是返回 value foreach is not a member of AnyRef
。我需要告诉 el.get("samples") 类型,但我不知道如何告诉它是一个复杂的 Map 对象。
好吧,Casbah 文档真的很糟糕。我实际上是通过阅读专门在 Scala for Data Science 中使用 MongoDB with Scala 的章节来了解这一点的。
我的代码如下:
val dbSamples = db("samples").find(MongoDBObject("name" -> name))
var timesSeries: Array[TimeSeries] = Array()
for (s <- dbSamples) {
var tSeries_seq: Array[Array[Double]] = Array()
val measures = s.getAs[List[DBObject]]("samples").get
for (m <- measures) {
println(m.getAs[DBObject]("raw").get.getAs[List[Double]]("accel").get.toArray)
tSeries_seq = tSeries_seq :+ m.getAs[DBObject]("raw").get.getAs[List[Double]]("accel").get.toArray
}
timesSeries = timesSeries :+ new TimeSeries(tSeries_seq, s.getAs[Integer]("valid").get)
}
timesSeries