MongoDB_pymongo
python使用pymongo访问MongoDB的基本操作
安装pymongo: pip install pymongo
from pymongo import MongoClient
import datetime
from pprint import pprint #连接
#client = MongoClient('localhost',27017)
client = MongoClient('mongodb://root:123@localhost:27017') #使用数据库
db = client['db1'] #查看数据库下所有的集合
print(db.collection_names(include_system_collections=False)) #创建集合
table_user = db['userinfo'] #插入文档
user0 = {
'id':1,
'name':'lary',
'birth':datetime.datetime.now(),
'age':10,
'hobbies':['music','read','dancing'],
'addr':{
'country':'China',
'city':'BJ'
}
}
user1 = {
'id':2,
'name':'lary1',
'birth':datetime.datetime.now(),
'age':10,
'hobbies':['music','read','dancing'],
'addr':{
'country':'China',
'city':'BJ'
}
} user2 = {
'id':3,
'name':'lary2',
'birth':datetime.datetime.now(),
'age':10,
'hobbies':['music','read','dancing'],
'addr':{
'country':'China',
'city':'BJ'
}
} #插入数据
# res = table_user.insert_many([user0,user1,user2]).inserted_ids
# print(table_user.count()) #查找数据
#pprint(table_user.find_one())
# for item in table_user.find():
# pprint(item) print(table_user.find_one({'id':{'$gte':1},'name':'lary'})) #更新数据
table_user.update({'id':1},{'name':'lary'}) #传入新的文档替换旧的文档
table_user.save(
{
'id':2,
'name':'lary_test'
}
)
MongoDB_pymongo的更多相关文章
随机推荐
- 将现有硬盘(分区)无损创建为RAID1
背景 如果现在有一块硬盘(分区)正在使用,如果要设置成RAID1,并不需要将数据拷出,然后创建RAID1. 可以先将此硬盘设置成降级RAID1,然后添加新硬盘再激活RAID1即可,整个过程数据无损. ...
- CSS学习笔记之框模型
1.概述 为了更好的处理 元素内容.内边距.边框 和 外边距 之间的关系,CSS 定义了框模型如下: 内边距.边框 和 外边距 的默认值都是零,可以通过设置元素的 padding.border 和 m ...
- mysql中使用order 出现错误
- HDU 1569 方格取数(2)
方格取数(2) Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 15 ...
- 消息队列Rabbit安装
先安装elang 再安装Rabbit *Rabbit安装路径不能有空格 安装完成
- RestTemplate使用详解
1.RestTemplate添加RequestHeader如content-type可通过httpclient设置 List<Header> headers = new ArrayList ...
- 生成字符Banner
生成字符Banner http://patorjk.com/software/taag __ _______/ |_ ____ ____ ____ / ___/\ __\/ _ \ / \ / _ \ ...
- [Javascript Crocks] Apply a function in a Maybe context to Maybe inputs (curry & ap & liftA2)
Functions are first class in JavaScript. This means we can treat a function like any other data. Thi ...
- Narrow Art Gallery
Time Limit: 4000ms, Special Time Limit:10000ms, Memory Limit:65536KB Total submit users: 11, Accepte ...
- Xmind8破解激活
1.下载安装包: https://www.xmind.cn/download/ 进行安装 2.下载破解补丁: https://stormxing.oss-cn-beijing.aliyuncs.com ...