【问题标题】:How to set/modify value of a collection field from MongoDB Atlas triggers?如何从 MongoDB Atlas 触发器中设置/修改集合字段的值?
【发布时间】:2020-04-21 10:19:00
【问题描述】:

我在 Atlas 中有一个 MongoDB 和一个包含 createdAt/lastModifiedAt 字段的集合。我想设置触发器来自动处理这些字段。以下是我设置 createdAt 的触发器:

const collection = context.services.get("comand-dev").db("test").collection("ownerDetails");
const docId = changeEvent.documentKey._id;
collection;
collection.update(
  {_id : docId} ,
  { 
    $set : 
     {
       createdAt: Date()
     } 
  }
);

此触发函数抛出以下错误,我不知道我需要做什么才能成功触发:

TypeError: 'update' is not a function
> trace: 
TypeError: 'update' is not a function
    at exports (function.js:44:3)
    at apply (<native code>)
    at function_wrapper.js:2:3
    at <anonymous>:12:1

【问题讨论】:

    标签: mongodb mongodb-atlas


    【解决方案1】:

    根据documentation,您应该使用updateOneupdateMany

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-15
      • 1970-01-01
      • 2020-08-02
      • 2021-01-15
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多