Python操作MongoDB数据库PyMongo库使用方法
引用PyMongo
>>> import pymongo
创建连接Connection
>>> import pymongo
>>> conn = pymongo.Connection('localhost',27017)
或
>>> from pymongo import Connection
>>> conn = Connection('localhost',27017)
创建Connection时,指定host及port参数
>>> import pymongo
>>> conn = pymongo.Connection(host='127.0.0.1',port=27017)
连接数据库
>>> db = conn.ChatRoom
或
>>> db = conn['ChatRoom']
连接聚集
>>> account = db.Account
或
>>> account = db["Account"]
查看全部聚集名称
>>> db.collection_names()
查看聚集的一条记录
>>> db.Account.find_one()
>>> db.Account.find_one({"UserName":"keyword"})
查看聚集的字段
>>> db.Account.find_one({},{"UserName":1,"Email":1})
{u'UserName': u'libing', u'_id': ObjectId('4ded95c3b7780a774a099b7c'), u'Email': u'libing@35.cn'}
>>> db.Account.find_one({},{"UserName":1,"Email":1,"_id":0})
{u'UserName': u'libing', u'Email': u'libing@35.cn'}
查看聚集的多条记录
>>> for item in db.Account.find():
item
>>> for item in db.Account.find({"UserName":"libing"}):
item["UserName"]
查看聚集的记录统计
>>> db.Account.find().count()
>>> db.Account.find({"UserName":"keyword"}).count()
聚集查询结果排序
>>> db.Account.find().sort("UserName") --默认为升序
>>> db.Account.find().sort("UserName",pymongo.ASCENDING) --升序
>>> db.Account.find().sort("UserName",pymongo.DESCENDING) --降序
聚集查询结果多列排序
>>> db.Account.find().sort([("UserName",pymongo.ASCENDING),("Email",pymongo.DESCENDING)])
添加记录
>>> db.Account.insert({"AccountID":21,"UserName":"libing"})
修改记录
>>> db.Account.update({"UserName":"libing"},{"$set":{"Email":"libing@126.com","Password":"123"}})
删除记录
>>> db.Account.remove() -- 全部删除
>>> db.Test.remove({"UserName":"keyword"})

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Regarding the problem of removing the Python interpreter that comes with Linux systems, many Linux distributions will preinstall the Python interpreter when installed, and it does not use the package manager...

This article describes how to build a highly available MongoDB database on a Debian system. We will explore multiple ways to ensure data security and services continue to operate. Key strategy: ReplicaSet: ReplicaSet: Use replicasets to achieve data redundancy and automatic failover. When a master node fails, the replica set will automatically elect a new master node to ensure the continuous availability of the service. Data backup and recovery: Regularly use the mongodump command to backup the database and formulate effective recovery strategies to deal with the risk of data loss. Monitoring and Alarms: Deploy monitoring tools (such as Prometheus, Grafana) to monitor the running status of MongoDB in real time, and

Pylance type detection problem solution when using custom decorator In Python programming, decorator is a powerful tool that can be used to add rows...

About Pythonasyncio...

Using python in Linux terminal...

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...
