# coding=utf-8
import pymongo
from bson.objectid import ObjectId
my_client = pymongo.MongoClient("mongodb://localhost:27017/")
my_db = my_client["mixintu"] # 指定一个数据库
print(my_client.list_database_names( ))
collection = my_db["ddd"] # 字典集合
# 插入数据-------------------------------------------------
ddd01 = {
"id" : "20191013",
"name" : "Joran",
"age" : 20,
"gender": "male",
}
# 插入一条数据-------------------------------------------------------------------------------------
# result = collection.insert_one(ddd01)
# print(result.inserted_id) # 5da33f97f645c2ab41660ff8
ddd02 = {
"id" : "20191014",
"name" : "mixintu",
"age" : 21,
"gender": "male02",
}
# 插入多条数据------------------------------------------------------------------------------------------
# result = collection.insert_many([ddd01, ddd02])
# print(result.inserted_ids) # [ObjectId('5da33fbba7096e7dca0379a5'), ObjectId('5da33fbba7096e7dca0379a6')]
# 查询一条数据-----------------------------------------------------------------------------------------
# result = collection.find_one( )
# print(result) # {'_id': ObjectId('5da3409f11e87690d44ec8cf'), 'id': '20191013', 'name': 'Joran', 'age': '20',
# 'gender': 'male'}
# result = collection.find_one({'_id': ObjectId('5da3409f11e87690d44ec8cf')})
# print(result) # {'_id': ObjectId('5da3409f11e87690d44ec8cf'), 'id': '20191013', 'name': 'Joran', 'age': '20',
# 'gender': 'male'}
# 无条件查询----------------------------------------------------------------------------------------------
# for result in collection.find():
# print(result)
# 条件为age等于20-------------------------------------------------------------------------------------
# for result in collection.find({"age": 20}):
# print(result)
# 高级查询不等于20岁---------------------------------------------------------------------------------
# $lt $gt $lte $gte $ne $in $nin
# for result in collection.find({"age": {"$ne": 20}}):
# print(result)
# 使用正则表达式进行筛选——》查找以字母“m”开头的数据
# $regex匹配正则表达式 $exists属性是否存在 $type类型判断 $mod数字模操作 $text文本查询 $where高级条件查询
# for result in collection.find({"gender": {"$regex": "^m.*"}}):
# print(result)
# 排序-------------------------------------------------------------------------------------------------
# for result in collection.find( ).sort("age", 1): # 升序
# print(result)
# for result in collection.find( ).sort("age", -1): # 降序
# print(result)
# 偏移------------------------------------------------------------------------------------------------
# for result in collection.find( ).sort("age", 1).skip(2): # 升序并忽略前面两个元素
# print(result)
# for result in collection.find( ).sort("age", 1).skip(2).limit(2): # 升序并忽略前面两个元素,取出两条数据
# print(result)
# for result in collection.find( ).sort("age", -1).skip(2): # 降序并忽略前面两个元素
# print(result)
# 删除数据-------------------------------------------------------------------------------------------
# collection.delete_one({"age": 20}) # 删除age等于20的某一条数据
# collection.delete_many({"gender": {"$regex": "^m.*"}}) # 删除以字母“m”开头的数据
# collection.delete_many({}) # 删除集合中的所有数据
# 删除集合------------------------------------------------------------------------------------------
# collection.drop( ) # 成功删除后返回true,如果集合不存在,返回false。
# 更新数据------------------------------------------------------------------------------------------
# my_query = {"gender": "male02"} # 原来的
# new_values = {"$set": {"gender": "Canyon 123"}} # 要修改的($set操作符)
# result = collection.update_one(my_query, new_values)
# print(result.matched_count, result.modified_count) # 匹配的条数,影响的条数
# my_query = {"gender": {"$regex": "^m.*"}} # 原来的
# new_values = {"$set": {"gender": "Canyon 123"}} # 要修改的($set操作符)
# result = collection.update_many(my_query, new_values)
# print(result.matched_count, result.modified_count) # 匹配的条数,影响的条数
mongoDB 基本认识
最新推荐文章于 2025-07-13 19:25:24 发布
771

被折叠的 条评论
为什么被折叠?



