查询 MongoDB Atlas
您可以使用内置数据源客户端通过函数处理链接的MongoDB数据源中的数据。 该客户端包含允许读取、写入和聚合数据的方法。
有关如何在函数中使用数据源的示例,请参阅以下指南:
有关可用查询和聚合方法的详细参考信息,请参阅 MongoDB API 参考。
exports = async function() { // 1. Get a data source client const mongodb = context.services.get("mongodb-atlas"); // 2. Get a database & collection const db = mongodb.db("myDatabase") const collection = db.collection("myCollection") // 3. Read and write data with MongoDB queries await collection.insertOne({ text: "Hello, world!", author: context.user.id }) return await collection.findOne({ author: context.user.id }) }