简体   繁体   English

如何从 MongoDB Atlas 触发器中设置/修改集合字段的值?

[英]How to set/modify value of a collection field from MongoDB Atlas triggers?

I have a MongoDB in Atlas and a collection that has createdAt/lastModifiedAt fields.我在 Atlas 中有一个 MongoDB 和一个已createAt/lastModifiedAt 字段的集合。 I would like to set up triggers to automatically handle these fields.我想设置触发器来自动处理这些字段。 The following is my trigger for setting createdAt:以下是我设置 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()
     } 
  }
);

This trigger function throws the following error and I don't know what I need to do to have a successfull trigger:此触发器 function 引发以下错误,我不知道我需要做什么才能成功触发:

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

According to documentation you should be using updateOne or updateMany .根据文档,您应该使用updateOneupdateMany

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MongoDB Atlas触发器-访问不同的集合 - MongoDB Atlas triggers - access different collection 如何在MongoDB Atlas集合文档中读取值或字段并使用javascript,express和Node.js在邮递员中将结果显示为json - How to read the value or a field in mongodb atlas collection document and display results as json in postman using javascript,express and nodejs 如何使用另一个集合中的字段值更新Mongodb集合中的文档 - How to update document in Mongodb collection with a field value from another collection 根据另一个集合字段值设置一个 MongoDB 集合字段值 - Set the a MongoDB collection field value based on another collection field value 如果字符串在集合的字段值中,如何从集合中选择一个字符串 - MongoDB 聚合 - How to pick a string from set if string is in field value of collection - MongoDB aggregation 如何将json文件中的数据导入mongodb图集集合 - How to import data from json file to mongodb atlas collection MongoDB Atlas 触发器未设置字段 - MongoDB Atlas trigger does not set field 如何独立于字段名称在mongoDB集合中查找值? - How to find value in a mongoDB collection, independently from the field name? 如何从 Java 中的 MongoDB 集合中检索特定字段的值 - How to retrieve value of a specific field from a MongoDB collection in Java 如何在 Atlas MongoDb 上使用 typeorm 创建数据库集合? - How to create database collection with typeorm on Atlas MongoDb?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM