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的更多相关文章
随机推荐
- 微信系列之公众号Token验证
微信系列之公众号Token验证 pycharm连接线上服务器开发 开发过程笔记 参考资料 python3安装web.py可以选择安装`pip install web.py==0.40.dev0 pyc ...
- Number Puzzle
Number Puzzle Time Limit: 2 Seconds Memory Limit: 65536 KB Given a list of integers (A1, A2, .. ...
- rmq问题和lca可以相互转化
Sparse Table算法 一般RMQ的Sparse Table(ST)算法是基于倍增思想设计的O(Nlog2N) – O(1)在线算法 算法记录从每个元素开始的连续的长度为2k的区间中元素的最小值 ...
- 日志输出最不重要的就是控制台输出,控制台输出就是system.out而已
1.日志输出最不重要的就是控制台输出,控制台输出就是system.out而已 2.所以日志输出时候会存在一个Bug就是:stdout要配置在日志输出的最前面,因为stdout控制台输出,最不重要,如果 ...
- Python学习-修饰器 - itemgetter的妙用
下面这篇对装饰器讲的很好,懂了. http://python.jobbole.com/85056/ <简单 12 步理解 Python 装饰器> 使用装饰器非常简单(见步骤10),但是写装 ...
- 链表快排 & 基于链表的排序
以前只知道链表做插入(朴素.非二分)排序挺方便的.现在知道了(单)链表进行快速排序也是很好的(只是跟一般的快排的方式不一样). 参考: http://blog.csdn.net/otuhacker/a ...
- POJ 2607
一次FLOYD,再枚举. 注意题目要求的输出是什么哦. #include <iostream> #include <cstdio> #include <cstring&g ...
- [Javascript Crocks] Recover from a Nothing with the `coalesce` Method
The alt method allows us to recover from a nothing with a default Maybe, but sometimes our recovery ...
- [iOS]UITableViewController完毕收回键盘操作
UITableViewController 本身可以实现键盘适配(cell中控件焦点会移动到键盘上方 在做键盘收回的时候思考过例如以下方案 1.tableview加入点击事件 结果:点击事件和tabl ...
- POJ 2553 The Bottom of a Graph(强连通分量)
POJ 2553 The Bottom of a Graph 题目链接 题意:给定一个有向图,求出度为0的强连通分量 思路:缩点搞就可以 代码: #include <cstdio> #in ...