gpt4 book ai didi

mongodb - 更新 meteor mongo 中的子文档

转载 作者:可可西里 更新时间:2023-11-01 09:29:37 28 4
gpt4 key购买 nike

我正在处理需要更新 meteor mongo 中的子文档的查询。我的代码如下所示:

 Cases.update(
{"_id":doc._id,"notes._id": doc.noteid},
{$set:{
'notes.$': {
'note': doc.note,
'updatedBy': currentUser,
'updatedAt': date
}
}});

这确实有效,但是它确实删除了不在更新中的其他字段,例如“createdAt”和“date”。我这样做了,但我得到了相同的结果:

Cases.update(
{"_id":doc._id,"notes._id": doc.noteid},
{$set:{
'notes.$': {
'note': doc.note,
'updatedBy': currentUser,
'updatedAt': date,
'createdBy':doc.createdBy,
'date': doc.date,
'_id':doc.noteid
}
}});

操作中删除了 notes.$._id、date 和 createdBy 字段。我正在使用 aldeed 简单模式和集合 2 作为 autoForm 包的包。但是,此更新是使用带有 Meteor.call 的通用表单完成的。我正在使用 meteor 1.2.1。我知道集合 2 的 removeEmptyString 设置在默认情况下处于打开状态,这就是为什么我尝试以第二种方式进行操作但它仍然不起作用的原因。有人可以帮忙吗?

最佳答案

你的 $set 是说,“将 notes.$ 对象分配给我给你的这个对象,并删除当前存在的任何内容。”

你想做这样的事情:

Cases.update( {"_id":doc._id, "notes._id": doc.noteid},
{
$set:{
'notes.$.note': doc.note,
'notes.$.updatedBy': currentUser,
'notes.$.updatedAt': date
}
});

我的 $set 是说,“将 notes.$ 对象的这三个特定字段分配给我给你的值,不要碰其他任何东西。”

关于mongodb - 更新 meteor mongo 中的子文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35164101/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com