我是一个尝试 MongoDB-atlas 免费套餐的新手。我有一个名为 - “mongoCluster”的集群。在它下面,我有一个数据库 - “testdb”,在它下面有一个集合 - “testcollection”。该集合包含文档。从我的 java 应用程序中插入和读取工作正常。
现在我正在尝试在 MongoDB atlas 中创建一个新的计划触发器。但当我运行以下两行时,出现以下错误。对于任何其他 mongo 查询(如删除、更新和插入)也是如此。
代码:
exports = function() {
const collection=context.services.get("mongoCluster").db("testdb").collection("testcollection");
collection.insertOne({"a": "b"});
// const doc = collection.findOne();
};
Run Code Online (Sandbox Code Playgroud)
错误:
> ran on Mon Nov 16 2020 18:52:52 GMT-0800 (Pacific Standard Time)
> took 272.591178ms
> error:
TypeError: Cannot access member 'db' of undefined
> trace:
TypeError: Cannot access member 'db' of undefined
at exports (function.js:24:24)
at apply (<native code>)
at function_wrapper.js:3:1
at <anonymous>:8:1
Run Code Online (Sandbox Code Playgroud)
查看错误,要么服务名称不正确(“未定义错误”),要么我缺少服务/数据库/集合的一些权限(“匿名:8:1 错误”)。
我在 …