我想通过在数组的第一个位置添加一个新元素来更新文档。
我实际上在做什么:
val updateRequest = myCollection.findOneAndUpdate(and(equal("site_id", new ObjectId(siteId)),and(equal("image_name", imageName))),
addToSet("url_history", urlHistory))
结果 :
{
"_id" : ObjectId("5e297ea0c7ede90a7ae7586e"),
"image_name" : "test.jpg",
"url_history" : [
{
"date" : ISODate("2020-02-08T10:43:47.127Z"),
"url" : "No image url"
},
{
"date" : ISODate("2020-02-11T10:43:47.127Z"),
"url" : "test.jpg"
}
]
}
预期结果:
{
"_id" : ObjectId("5e297ea0c7ede90a7ae7586e"),
"image_name" : "test.jpg",
"url_history" : [
{
"date" : ISODate("2020-02-11T10:43:47.127Z"),
"url" : "test.jpg"
},
{
"date" : ISODate("2020-02-08T10:43:47.127Z"),
"url" : "No image url"
}
]
}
我终于找到了解决方案!
这里是 :
val updateRequest = myCollection.findOneAndUpdate(and(equal("site_id", new ObjectId(siteId)),and(equal("image_name", imageName))),
pushEach("url_history", PushOptions().position(0), urlHistory))
我使用了PushOptions。
希望能帮助到你。
来源:https : //mongodb.github.io/mongo-scala-driver/2.6/scaladoc/org/mongodb/scala/model/Updates$.html
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句