PyMongo使用入门(一)
来源:互联网 发布:java 接口简写 编辑:程序博客网 时间:2024/06/06 12:24
安装
pip install pymongo
python3.4环境配置好,我是Windows系统,cmd命令安装就可以了
连接数据库,简单增加,查询数据
import pymongofrom pymongo import MongoClientfrom bson.objectid import ObjectIdimport datetimeclient = MongoClient('localhost', 27017)db = client.PymongoDemo"""第二种连接方法"""#client = MongoClient('mongodb://localhost:27017/')#db = client['test-database']#collection = db['test-collection']post = {"author": "Mike", "text": "My first blog post!", "tags": ["mongodb", "python", "pymongo"], "date": datetime.datetime.utcnow()}posts = db.posts"""要插入到文档的集合,我们可以使用insert_one()方法:"""post_id = posts.insert_one(post).inserted_idprint(post_id)"""我们列出数据库所有的集合"""db.collection_names(include_system_collections=False)print(posts.find_one())print(posts.find_one({"author": "Mike"}))print(posts.find_one({"author": "Eliot"}))#根据ObjectId查询posts.find_one({"_id": post_id})"""在Web应用程序中的一个常见的任务是让从请求的URL获得ObjectId来查找匹配的文件。在这种情况下,有必要将其传递给find_one前,从一个字符串转换成ObjectId:"""def get(post_id): document = client.db.collection.find_one({'_id': ObjectId(post_id)})
批量插入
import pymongofrom pymongo import MongoClientfrom bson.objectid import ObjectIdimport datetimeclient = MongoClient('localhost', 27017)db = client.PymongoDemoposts = db.posts#批量插入new_posts = [{"author": "Mike", "text": "Another post!", "tags": ["bulk", "insert"], "date": datetime.datetime(2009, 11, 12, 11, 14)}, {"author": "Eliot", "title": "MongoDB is fun", "text": "and pretty easy too!", "date": datetime.datetime(2009, 11, 10, 10, 45)}]result = posts.insert_many(new_posts)print(result.inserted_ids)
查询
import pymongofrom pymongo import MongoClientfrom bson.objectid import ObjectIdimport datetimeclient = MongoClient('localhost', 27017)db = client.PymongoDemoposts = db.postsfor post in posts.find(): print(post)for post in posts.find({"author": "Mike"}): print(post)#统计print(posts.count())"""范围查询"""d = datetime.datetime(2009, 11, 12, 12)for post in posts.find({"date": {"$lt": d}}).sort("author"): print(post)"""索引""""""创建唯一索引""""""一种是在_id的索引MongoDB的自动创建,另一种是我们刚创建的用户ID的索引。"""#result = db.profiles.create_index([('user_id', pymongo.ASCENDING)], unique=True)#print(list(db.profiles.index_information()))user_profiles = [ {'user_id': 211, 'name': 'Luke'}, {'user_id': 212, 'name': 'Ziltoid'}]result = db.profiles.insert_many(user_profiles)new_profile = {'user_id': 213, 'name': 'Drew'}duplicate_profile = {'user_id': 212, 'name': 'Tommy'}result = db.profiles.insert_one(new_profile) # 这可以"""重复键错误"""result = db.profiles.insert_one(duplicate_profile) #报错
0 0
- PyMongo使用入门(一)
- PyMongo使用入门(二)
- PyMongo使用入门(三)
- PyMongo使用入门(四)
- PyMongo使用入门(五)
- PyMongo使用入门(六)
- PyMongo使用入门(七)
- Pymongo Tutorial & Pymongo入门教
- pymongo使用
- pymongo 使用
- mongoDB-pymongo-入门
- 初窥Python(一)——使用pymongo连接MongoDB
- 初窥Python(一)——使用pymongo连接MongoDB
- pymongo(1) pymongo的基本使用
- pymongo使用问题汇总
- PyMongo基本使用
- PyMongo基本使用
- pymongo 使用小结
- Java反射初探
- 【u031】租用游艇
- Nginx虚拟主机及相关设置
- xutils 使用规则以及所实现的功能
- http 响应头里content-length 的几种情况
- PyMongo使用入门(一)
- C语言判断两个浮点数是否相等
- 泛型以及泛型的擦除
- ls指令
- VS 2010 创建新项目 解决方案失败
- PLSQL中的类型与PLS_INTEGER
- Mariadb(mysql)数据库管理系统-01——安装、库管理
- Map集合
- 利用Jsoup爬取网页内容