在lambda中使用Python将数组值插入MongoDB中的单独文档,可以通过以下步骤实现:
pip install pymongo
import pymongo
def lambda_handler(event, context):
# MongoDB连接信息
host = 'mongodb.example.com'
port = 27017
database = 'mydatabase'
username = 'myusername'
password = 'mypassword'
# 创建MongoDB连接
client = pymongo.MongoClient(host, port)
db = client[database]
db.authenticate(username, password)
def lambda_handler(event, context):
# ...
# 定义要插入的数组值
array_values = [1, 2, 3, 4, 5]
# 插入数组值到MongoDB的集合中
collection = db['mycollection']
document = {'array_field': array_values}
collection.insert_one(document)
import pymongo
def lambda_handler(event, context):
# MongoDB连接信息
host = 'mongodb.example.com'
port = 27017
database = 'mydatabase'
username = 'myusername'
password = 'mypassword'
# 创建MongoDB连接
client = pymongo.MongoClient(host, port)
db = client[database]
db.authenticate(username, password)
# 定义要插入的数组值
array_values = [1, 2, 3, 4, 5]
# 插入数组值到MongoDB的集合中
collection = db['mycollection']
document = {'array_field': array_values}
collection.insert_one(document)
这样,当Lambda函数被触发时,它将连接到MongoDB,并将数组值插入到指定的集合中。请根据实际情况修改MongoDB的连接信息、数组值和集合名称。
领取专属 10元无门槛券
手把手带您无忧上云