@1mill/mongo 中文文档教程
@1mill/mongo
跨 lambda 调用缓存 MongoDB 连接
npm install @1mill/mongo
const { Mongo } = require('@1mill/mongo')
const mongo = new Mongo({
db: 'my-database-name',
uri: 'mongodb://my-username:my-password@my-mong-host-name:27017/my-database-name',
})
exports.handler = async (cloudevent = {}, ctx = {}) {
ctx.callbackWaitsForEmptyEventLoop = false
const { db } = await mongo.connect()
const collection = db.collection('my-collection-name')
await collection.createIndex({ something: 1 })
await collection.insertOne({ something: 'yes' })
}
Name | Required | Default | Notes |
---|---|---|---|
db | process.env.MILLMONGODB | ||
options | {} | useNewUrlParser and useUnifiedTopology enabled by default | |
uri | yes | process.env.MILLMONGOURI |
@1mill/mongo
Cache MongoDB connections across lambda invokations
npm install @1mill/mongo
const { Mongo } = require('@1mill/mongo')
const mongo = new Mongo({
db: 'my-database-name',
uri: 'mongodb://my-username:my-password@my-mong-host-name:27017/my-database-name',
})
exports.handler = async (cloudevent = {}, ctx = {}) {
ctx.callbackWaitsForEmptyEventLoop = false
const { db } = await mongo.connect()
const collection = db.collection('my-collection-name')
await collection.createIndex({ something: 1 })
await collection.insertOne({ something: 'yes' })
}
Name | Required | Default | Notes |
---|---|---|---|
db | process.env.MILLMONGODB | ||
options | {} | useNewUrlParser and useUnifiedTopology enabled by default | |
uri | yes | process.env.MILLMONGOURI |