首页 > 数据库技术 > 详细

Python——连接MongoDB

时间:2018-01-28 15:08:03      阅读:217      评论:0      收藏:0      [点我收藏+]

建立与MongoDB之间的连接:官方文档

#coding:utf-8

import pymongo

client = pymongo.MongoClient(‘127.0.0.1‘,27017)     # 建立与MongoDB的连接
#有用户名和密码时:pymongo.MongoClient(‘mongodb://用户名:密码@localhost:27017/基于哪个数据库进行验证的‘)

db = client.xingedb     # 切换使用的数据库

# 增
# db.t1.insert_one({‘name‘:‘abc‘,‘age‘:18})
# insert_more

# 改
# db.t1.update_one({‘name‘:‘abc‘},{‘$set‘:{‘name‘:‘123‘}})
# update_more

# 删
# db.t1.delete_one({‘name‘:‘123‘})
# delete_more

# 查
# cursor = db.t1.find({‘age‘:{‘$gt‘:17}})     # 返回一个游标对象
# find_one
#
# 显示方法1:
# for i in cursor:
#     print(i)
#
# 方法2:
# cursor.next()
# cursor.next()
# cursor.next()

# sort,skip,limit方法
#
# cursor = db.t1.find({‘age‘:{‘$gt‘:17}}).sort(‘_id‘,-1).limit(1).skip(1)
#
# for i in cursor:
#     print(i)

# count方法
# print(db.t1.count())

 

Python——连接MongoDB

原文:https://www.cnblogs.com/x54256/p/8371176.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!