我有一个包含f1,f2和f3三个字段的数据库, 其中f3是一个数组。我想执行更新操作如下所示:
db.collection.update({f1:1},{$push:{f3:{$each:[f2的值],$slice:-2}}})
我在互联网上搜索了一下,但没有找到任何信息。我不确定这是否可能。非常感谢您的帮助。
示例:这是我的文档集合:
我希望文档更新user1的得分历史记录,使doc1变成如下格式:
我希望现在我的问题更加清晰明了。谢谢。
db.collection.update({f1:1},{$push:{f3:{$each:[f2的值],$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"] }
同样的更新应该将文档1 更改为:
doc1 = { name: "User1", score: "Good", History of scores: ["Average", "Bad", "Good", "Good"] }
我希望现在我的问题更加清晰明了。谢谢。