我有一个带有一组字段 f1、f2、f3 的数据库,其中 f3 是一个数组。我想做这样的更新操作
db.collection.update ({f1:1},{$push:{f3:{$each:[ f2's value ],$slice:-2}}})
我在互联网上搜索并没有找到任何东西。我不确定这是否可能。任何帮助将非常感激。
例子:
这是我的一组文件:
doc1 = { name: "User1", score: "Good", History of scores: ["Good", "Bad", "Average", "Bad"] }
doc2 = { name: "User2", score: "Bad", History of scores: ["Good", "Average", "Average", "Bad"] }
doc3 = { name: "User3", score: "Good", History of scores: ["Good", "Good", "Average", "Good"] }
现在假设我们必须插入相应的数据:
{name : "User1", score: "Good"}
我希望该文档更新 user1 的分数历史记录,以便 doc1 变为如下:
doc1 = { name: "User1", score: "Good", History of scores: ["Bad", "Average", "Bad", "Good"] }
另一个相同的更新应该将 doc1 更改为:
doc1 = { name: "User1", score: "Good", History of scores: ["Average", "Bad", "Good", "Good"] }
我希望现在我的问题变得更清楚了。谢谢。