使用Casbah查询深层嵌套MongoDB对象[Scala]

时间:2017-02-17 15:02:25

标签: mongodb scala casbah

  {'_ 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'}}]}
  




对于样本数组I中的每个样本需要提取数组加速度。我写了下面的代码




  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”)
  
&#xA;&#xA; < p>返回值foreach不是AnyRef 的成员。我需要告诉el.get(“samples”)类型,但我不知道如何判断它是一个复杂的Map对象。

&#xA;

1 个答案:

答案 0 :(得分:0)

好吧,所以Casbah文档非常糟糕。我实际上是通过阅读专用于在Scala中使用Scala for Data Science的MongoDB的章节来实现的。

我的代码如下:

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