基于Python的MongoDB使用

基于Python的MongoDB

import pymongo

# 创建客户端
client = pymongo.MongoClient('localhost')

#创建数据库
db = client['test0506']

#创建集合
collection = db['users']

#插入数据
collection.insert({"name":"song","age":18})
collection.insert({"name":"deng","age":22})
collection.insert({"name":"tang","age":21})
collection.insert({"name":"chen","age":22})

#插入多条数据
user1 = {'name':'zs','age':19}
user2 = {'name':'ls','age':17}
user3 = {'name':'ww','age':29}
collection.insert_many([user1,user2,user3])

#查找
user = collection.find({'age':22})
print(user)
for i in user:
    print(i)

猜你喜欢

转载自blog.csdn.net/qq_40576301/article/details/100553223
今日推荐