我想使用spark在mongodb中现有文档的嵌入字段中插入新条目。
示例
Blog {
id:"001"
title:"This is a test blog",
content:"...."
comments:[{title:"comment1",content:".."},{title:"comment2",content:"..."}]
}
我想做什么
db.blogs.update({id:"001"}, {$push:{comments:{title:"commentX",content:".."}}});
这个图书馆目前有可能吗?如果不是,请您指出正确的方向。
提前致谢
最佳答案
我可以使用Casbah库中的Spark-mongoDb进行所需的操作。
import java.sql.Timestamp
import java.util.Date
import com.mongodb.casbah.MongoClient
import com.mongodb.casbah.commons.MongoDBObject
import com.mongodb.casbah.query.Imports._
object TestCasbah {
def main(args: Array[String]) {
val mongoClient = MongoClient("172.18.96.45", 27017)
val db = mongoClient("agentCallRecord")
val coll = db("CallDetails")
val query = MongoDBObject("agentId" -> "agent_1")
val callRatingMongoObject = MongoDBObject("audioId" -> 12351,"startTime" -> new Timestamp(new Date().getTime).toString, "endTime" -> new Timestamp(new Date().getTime).toString, "totalScore" -> 1, "sentiment" -> "NEGATIVE")
val update = $push("callRating" -> callRatingMongoObject)
coll.update(query, update)
}
}
关于mongodb - spark-mongodb库中是否有更新+推送支持?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36867983/