在 Scala 中从 Mongo DB 收集文档字段到一个数组
Collecting document fields to an array from Mongo DB in Scala
所以我收集了 Mongo 数据库中按页面浏览量评分的新闻文章,我这样查询:
// To directly connect to the default server localhost on port 27017
val mongoClient: MongoClient = MongoClient("mongodb://localhost:27017/")
val database: MongoDatabase = mongoClient.getDatabase("Posts")
var collection: MongoCollection[Document] = database.getCollection("news")
collection.find(equal("id","id123")).limit(5).subscribe((doc: Document)=>println(s"${doc.get("views")}"))
这会打印:
Some(BsonInt32{value=66043})
Some(BsonInt32{value=66306})
Some(BsonInt32{value=66336})
Some(BsonInt32{value=66365})
Some(BsonInt32{value=66384})
所以现在我想将所有这些值收集到一个数组中,我试图通过这行代码来做到这一点:
var scores = collection.find(equal("id","id123")).limit(5).subscribe((doc: Document)=>doc.get("score").map(_.asInt32().getValue).collect())
但是.collect()
不起作用。
将 Mongo 字段转换为整数数组的最佳方法是什么?
我想出了一个解决方案,虽然我并不完全理解它:
import org.mongodb.scala._
import org.mongodb.scala.model.Filters._
import org.mongodb.scala.model.Projections._
object main extends App {
//Connect to Mongo Client and DB
val mongoClient: MongoClient = MongoClient("mongodb://localhost:27017/")
val database: MongoDatabase = mongoClient.getDatabase("MyDatabase")
//Get Collection
val collection: MongoCollection[Document] = database.getCollection("collectionName")
//Returns the object with specified fields in projection
//as a Future observable containing MongoDB Documents
val collScores = collection.find(equal("id","id123")).limit(5).projection(fields(include("id", "score", "time"), excludeId()))
//Maps the document fields to tuples and converts them
//to non BSon values since they are easier to work with
//than future observables.
val listMapScores = collScores.map(doc=>(doc("id").asString.getValue,doc("score").asInt32.getValue,doc("time").asDouble.getValue)).collect().results().head
}
我不确定为什么您需要在最后一行同时执行 results()
和 head
。如果您不接受 head
,您最终会得到 Seq[Seq(String,Int)]]
。据我所知,无论您在系列中使用 head
还是 Nth
项,未来的可观察值都包含相同的值。
所以我收集了 Mongo 数据库中按页面浏览量评分的新闻文章,我这样查询:
// To directly connect to the default server localhost on port 27017
val mongoClient: MongoClient = MongoClient("mongodb://localhost:27017/")
val database: MongoDatabase = mongoClient.getDatabase("Posts")
var collection: MongoCollection[Document] = database.getCollection("news")
collection.find(equal("id","id123")).limit(5).subscribe((doc: Document)=>println(s"${doc.get("views")}"))
这会打印:
Some(BsonInt32{value=66043})
Some(BsonInt32{value=66306})
Some(BsonInt32{value=66336})
Some(BsonInt32{value=66365})
Some(BsonInt32{value=66384})
所以现在我想将所有这些值收集到一个数组中,我试图通过这行代码来做到这一点:
var scores = collection.find(equal("id","id123")).limit(5).subscribe((doc: Document)=>doc.get("score").map(_.asInt32().getValue).collect())
但是.collect()
不起作用。
将 Mongo 字段转换为整数数组的最佳方法是什么?
我想出了一个解决方案,虽然我并不完全理解它:
import org.mongodb.scala._
import org.mongodb.scala.model.Filters._
import org.mongodb.scala.model.Projections._
object main extends App {
//Connect to Mongo Client and DB
val mongoClient: MongoClient = MongoClient("mongodb://localhost:27017/")
val database: MongoDatabase = mongoClient.getDatabase("MyDatabase")
//Get Collection
val collection: MongoCollection[Document] = database.getCollection("collectionName")
//Returns the object with specified fields in projection
//as a Future observable containing MongoDB Documents
val collScores = collection.find(equal("id","id123")).limit(5).projection(fields(include("id", "score", "time"), excludeId()))
//Maps the document fields to tuples and converts them
//to non BSon values since they are easier to work with
//than future observables.
val listMapScores = collScores.map(doc=>(doc("id").asString.getValue,doc("score").asInt32.getValue,doc("time").asDouble.getValue)).collect().results().head
}
我不确定为什么您需要在最后一行同时执行 results()
和 head
。如果您不接受 head
,您最终会得到 Seq[Seq(String,Int)]]
。据我所知,无论您在系列中使用 head
还是 Nth
项,未来的可观察值都包含相同的值。