[英]MongoDB Atlas trigger does not set field
I have the following trigger function that is not function.我有以下触发器 function 不是 function。 I would like to know why it does not set the field createdAt:
我想知道为什么它没有设置字段 createdAt:
const collection = context.services.get("comand-dev").db("test").collection("ownerDetails");
const docId = changeEvent.documentKey._id;
collection;
collection.updateOne(
{_id : docId} ,
{
$set :
{
createdAt: Date()
}
}
);
The trigger logs says OK but the field is not there触发器日志显示正常,但该字段不存在
This worked for me.这对我有用。 A small issue with the syntax.
语法的一个小问题。 you have to add the quotes.
你必须添加引号。
collection.updateOne(
{"_id" : docId} ,
{
"$set" :
{
"createdat": Date()
}
}
);
I was able to figure out so I would share my solution.我能够弄清楚,所以我会分享我的解决方案。 In my case, I convert _id object id to date and insert it as a new field for newly inserted document.
就我而言,我将 _id object id 转换为 date 并将其作为新插入文档的新字段插入。 The trigger will be configurated as insert trigger operation.
触发器将被配置为插入触发器操作。 Enable event ordering.
启用事件排序。
exports = function (changeEvent) {
const docId = changeEvent.documentKey._id;
console.log(docId);
const collection =
context.services.get("Cluster0").db("Driver").collection("Trip");
collection.updateOne({_id: docId },
[{
"$addFields" : {
"CreationDate" : {
"$toDate" : "$_id"
}
}
}],{upsert: false}
);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.