面试题答案
一键面试from pymongo import MongoClient
# 连接数据库
client = MongoClient('mongodb://localhost:27017/')
db = client['your_database_name'] # 替换为实际数据库名
users_collection = db['users']
# 插入数据
new_user = {'name': 'John', 'age': 30}
users_collection.insert_one(new_user)
# 查询年龄大于25岁的用户数据
result = users_collection.find({'age': {'$gt': 25}})
for user in result:
print(user)