Python与Mongodb交互

时间:2023-03-09 00:15:09
Python与Mongodb交互

MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统

MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案

MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组

下载安装

 curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz # 下载
tar -zxvf mongodb-linux-x86_64-3.0.6.tgz # 解压
mv mongodb-linux-x86_64-3.0.6/ /usr/local/mongodb # 将解压包拷贝到指定目录
export PATH=<mongodb-install-directory>/bin:$PATH #<mongodb-install-directory> 为Mongo的安装路径,如本文的 /usr/local/mongodb
mkdir -p /data/db #创建数据库目录(启动指定--dbpath)

配置文件

 mongod -f MongoDB.conf 指定配置文件(默认在/etc下寻找)

 基本配置
systemLog:
destination: file
path: /usr/local/var/log/mongodb/mongo.log
logAppend: true
storage:
dbPath: /usr/local/var/mongodb
net:
bindIp: 127.0.0.1
port: 11811

配置文件参数信息

 #数据库数据存放目录
dbpath=/usr/local/mongodb304/data
#数据库日志存放目录
logpath=/usr/local/mongodb304/logs/mongodb.log
#以追加的方式记录日志
logappend = true
#端口号 默认为27017
port=27017
#以后台方式运行进程
fork=true
#开启用户认证
auth=true
#关闭http接口,默认关闭http端口访问
nohttpinterface=true
#mongodb所绑定的ip地址
bind_ip = 127.0.0.1
#启用日志文件,默认启用
journal=true
#这个选项可以过滤掉一些无用的日志信息,若需要调试使用请设置为false
quiet=true 其他配置参数含义 --quiet # 安静输出
--port arg # 指定服务端口号,默认端口27017
--bind_ip arg # 绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定默认本地所有IP
--logpath arg # 指定MongoDB日志文件,注意是指定文件不是目录
--logappend # 使用追加的方式写日志
--pidfilepath arg # PID File 的完整路径,如果没有设置,则没有PID文件
--keyFile arg # 集群的私钥的完整路径,只对于Replica Set 架构有效
--unixSocketPrefix arg # UNIX域套接字替代目录,(默认为 /tmp)
--fork # 以守护进程的方式运行MongoDB,创建服务器进程
--auth # 启用验证
--cpu # 定期显示CPU的CPU利用率和iowait
--dbpath arg # 指定数据库路径
--diaglog arg # diaglog选项 0=off 1=W 2=R 3=both 7=W+some reads
--directoryperdb # 设置每个数据库将被保存在一个单独的目录
--journal # 启用日志选项,MongoDB的数据操作将会写入到journal文件夹的文件里
--journalOptions arg # 启用日志诊断选项
--ipv6 # 启用IPv6选项
--jsonp # 允许JSONP形式通过HTTP访问(有安全影响)
--maxConns arg # 最大同时连接数 默认2000
--noauth # 不启用验证
--nohttpinterface # 关闭http接口,默认关闭27018端口访问
--noprealloc # 禁用数据文件预分配(往往影响性能)
--noscripting # 禁用脚本引擎
--notablescan # 不允许表扫描
--nounixsocket # 禁用Unix套接字监听
--nssize arg (=16) # 设置信数据库.ns文件大小(MB)
--objcheck # 在收到客户数据,检查的有效性,
--profile arg # 档案参数 0=off 1=slow, 2=all
--quota # 限制每个数据库的文件数,设置默认为8
--quotaFiles arg # number of files allower per db, requires --quota
--rest # 开启简单的rest API
--repair # 修复所有数据库run repair on all dbs
--repairpath arg # 修复库生成的文件的目录,默认为目录名称dbpath
--slowms arg (=100) # value of slow for profile and console log
--smallfiles # 使用较小的默认文件
--syncdelay arg (=60) # 数据写入磁盘的时间秒数(0=never,不推荐)
--sysinfo # 打印一些诊断系统信息
--upgrade # 如果需要升级数据库 主/从参数
-------------------------------------------------------------------------
--master # 主库模式
--slave # 从库模式
--source arg # 从库 端口号
--only arg # 指定单一的数据库复制
--slavedelay arg # 设置从库同步主库的延迟时间 Replicaton 参数
--------------------------------------------------------------------------------
--fastsync # 从一个dbpath里启用从库复制服务,该dbpath的数据库是主库的快照,可用于快速启用同步
--autoresync # 如果从库与主库同步数据差得多,自动重新同步,
--oplogSize arg # 设置oplog的大小(MB)

启动mongodb

 ./mongod --dbpath=/data/db -f MongoDB.conf --rest
# 默认端口为:27017
# MongoDB 提供了简单的 HTTP 用户界面。 如果你想启用该功能,需要在启动的时候指定参数 --rest
# MongoDB 的 Web 界面访问端口比服务的端口多1000。如果你的#MongoDB运行端口使用默认的27017,你可以在端口号为28017访问web用户界面,即地址为:http://localhost:28017

连接mongodb

 # sudo mongo
# sudo mongo --port 11811
# sudo mongo -u root -p pwd 127.0.0.1:11811/test

创建管理员

 > use admin
switched to db admin
> db
admin
> db.createUser({user:'admin',pwd:'',roles:[{role:'userAdminAnyDatabase',db:'admin'}]})
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
> exit

创建普通用户

 > use mydb
switched to db mydb
> db.createUser({user:'guest',pwd:'',roles:[{role:'readWrite',db:'mydb'}]})
Successfully added user: {
"user" : "guest",
"roles" : [
{
"role" : "readWrite",
"db" : "mydb"
}
]
}
> db.auth('guest','')
1

删除用户

 > db.dropUser("guest")
true

查看存在用户

 > db.system.users.find()
{ "_id" : "admin.suoning", "user" : "suoning", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "jykZ+hm5QLhfPDKvcOWyZw==", "storedKey" : "uBr5nVjGLGYq0EdKyosDYOl3HA8=", "serverKey" : "S58tTedpS0QvvxanautLsKXc/OY=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
{ "_id" : "admin.guest", "user" : "guest", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "+pf1zZC1jaiM+GOMZs5qOg==", "storedKey" : "0ul1QMSdcEwwPVB5cq/GriwarCQ=", "serverKey" : "DLLEWO+NAwUd1nwnmLSp3tFpq/8=" } }, "roles" : [ { "role" : "readWrite", "db" : "mydb" } ] }
{ "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "treTBfONTUztxZLy1AU9XA==", "storedKey" : "0IsEBotj0WzElFbzv3CuNRiVix8=", "serverKey" : "gNDkhP+U0ML4P0TGf0pI+F3w3/8=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }

数据库角色

内建的角色

数据库用户角色:read、readWrite;
Read:允许用户读取指定数据库
readWrite:允许用户读写指定数据库 数据库管理角色:dbAdmin、dbOwner、userAdmin;
dbAdmin:允许用户在指定数据库中执行管理函数,如索引创建、删除,查 看统计或访问system.profile
userAdmin:允许用户向system.users集合写入,可以找指定数据库里创建、删除和管理用户 集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;
clusterAdmin:只在admin数据库中可用,赋予用户所有分片和复制集相关函数的管理权限 备份恢复角色:backup、restore; 所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
readAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读权限
readWriteAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读写权限
userAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的userAdmin权限
dbAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的dbAdmin权限。 超级用户角色:root // 这里还有几个角色间接或直接提供了系统超级用户的访问(dbOwner 、userAdmin、userAdminAnyDatabase) 内部角色:__system 创建超级管理员需要未开启权限模式的情况下执行;
如果 MongoDB 开启了权限模式,并且某一个数据库没有任何用户时,在不验证权限的情况下,可以创建一个用户,当继续创建第二个用户时,会返回错误,若想继续创建用户则必须登录;
用户只能在用户所在数据库登录,管理员需要通过admin认证后才能管理其他数据库

数据类型

数据类型    描述
String 字符串。存储数据常用的数据类型。在 MongoDB 中,UTF-8 编码的字符串才是合法的。
Integer 整型数值。用于存储数值。根据你所采用的服务器,可分为 32 位或 64 位。
Boolean 布尔值。用于存储布尔值(真/假)。
Double 双精度浮点值。用于存储浮点值。
Min/Max keys 将一个值与 BSON(二进制的 JSON)元素的最低值和最高值相对比。
Arrays 用于将数组或列表或多个值存储为一个键。
Timestamp 时间戳。记录文档修改或添加的具体时间。
Object 用于内嵌文档。
Null 用于创建空值。
Symbol 符号。该数据类型基本上等同于字符串类型,但不同的是,它一般用于采用特殊符号类型的语言。
Date 日期时间。用 UNIX 时间格式来存储当前日期或时间。你可以指定自己的日期时间:创建 Date 对象,传入年月日信息。
Object ID 对象 ID。用于创建文档的 ID。
Binary Data 二进制数据。用于存储二进制数据。
Code 代码类型。用于在文档中存储 JavaScript 代码。
Regular expression 正则表达式类型。用于存储正则表达式。

Python操作Mongodb模块

 pip install pymongo
or
easy_install install pymongo

操作方式

连接Mongodb

import pymongo

# 建立MongoDB数据库连接
# connection = pymongo.Connection('192.168.198.128', 27017) # 如果设置了权限,注意xxx用户权限要可以cover到后面使用到的数据库
# client = pymongo.MongoClient('192.168.198.128', 27017, username='guest', password='123456')
client = pymongo.MongoClient('192.168.198.128',27017) # 连接所需数据库,test为数据库名
db=client.test
# db_name = 'test'
# db = client[db_name] # 连接所用集合,也就是我们通常所说的表,test为表名
# db和collection都是延时创建的,在添加Document时才真正创建
collection=db.test

添加数据

first_name = ["陈","张","李","王","赵"]
second_name = ["冰","鑫","程","爱","暖"]
third_name = ["强","国","明","风","芬"]
data = [
{"_id":int(""+str(i)),
"name":random.choice(first_name)+
random.choice(second_name)+
random.choice(third_name),
"age":random.randint(16,60),
"high":random.randint(170,190),
"list":list(random.randint(1,200) for i in range(10))
} for i in range(5)
]
try:
for record in data:
collection.save(record)
except pymongo.errors.DuplicateKeyError:
print('record exists')
except Exception as e:
print(e)

删除数据

collection.delete_many({'age':{'$gt':20,'$lt':30}})   #删除所有满足条件的文档,删除_id大于6,小于100
collection.delete_one({'age':20}) #删除一条满足条件的文档,删除_id=6
#collection_set01.delete_many({}) #删除整个集合

更新数据

collection.replace_one({'_id': 10000}, {'name': '王宝宝'})                         #replace_one用指定的key-value替代原来所有的key-value
collection.update_one({"_id": {'$lt': 10008}}, {'$set': {"age": ""}}) #update_one更新已经对应的key-value,其它不变
collection.update_many({'_id': {'$gt': 10007}}, {'$set': {'age': ''}}) #同上,能够update所有符合匹配条件的文档

查询数据

print('\n------------身高小于180:')
print(type(collection.find({'high':{'$lt':180}})))
for row in collection.find({'high':{'$lt':180}}):
print(row)
print(type(collection.find_one({'high':{'$lt':180}})))
print('use find_one:',collection.find_one({'high':{'$lt':180}})['high'])
print('use find_one:',collection.find_one({'high':{'$lt':180}})) print('\n------------查询特定键')
print('------------查询身高大于170,并只列出_id,high和age字段(使用列表形式_id默认打印出来,可以使用{}忽视_id):')
for row in collection.find({'high':{'$gt':170}},projection=['high','age']):
print(row) print('\n------------skip参数用法')
for row in collection.find({'high':{'$gt':170}},['high','age'],skip=1):
print(row)
for row in collection.find({'high':{'$gt':170}},['high','age']).skip(1):
print(row) print('\n------------limit参数用法')
for row in collection.find({'high':{'$gt':170}},['high','age'],limit=1):
print(row) print('\n------------用{}描述特定键')
for row in collection.find({'high':{'$gt':170}},{'high':1,'age':1,'_id':False}):
print(row) print('\n------------多条件查询')
print(collection.find_one({'high':{'$gt':10},'age':{'$lt':26,'$gt':10}})) # for u in db.users.find({"age":{"$nin":(23, 26, 32)}}):
# print u
# select * from users where age not in (23, 26, 32) print('\n------------count')
print(collection.find({"age":{"$gt":20}}).count()) print('\n------------条件或')
print('大于等于29或者小于23')
for row in collection.find({"$or":[{"age":{"$lte":23}}, {"age":{"$gte":29}}]}):
print(row) print('\n------------exists')
for row in collection.find({'age':{'$exists':True}}):
print('age exists',row) # select * from 集合名 where exists 键1
for row in collection.find({'age':{'$exists':False}}):
print('age not exists',row) print('\n------------正则表达式查询')
print('method 1')
for row in collection.find({'name':{'$regex':r'.*暖.*'}}):
print(row)
print('method 2')
import re
Regex = re.compile(r'.*爱.*',re.IGNORECASE)
for row in collection.find({'name':Regex}):
print(row) print('\n------------使用sort排序(文档中没有排序的字段也会打印出来,表示最小)')
print('------------age 升序')
for row in collection.find().sort([["age",pymongo.ASCENDING]]):
print(row)
print('------------age 降序')
for row in collection.find().sort([("age",-1)]):
print(row)
print('------------age升序,high升序')
for row in collection.find().sort((("age",pymongo.ASCENDING),("high",pymongo.ASCENDING))):
print(row)
print('------------age升序,high降序')
for row in collection.find(sort=[("age",pymongo.ASCENDING),("high",pymongo.ASCENDING)]):
print(row) print('\n------------$all')
for row in collection.find({'list':{'$all':[77,117,165,37,57,49,178,90,3,166]}}):
print(row) print('\n------------$in')
for row in collection.find({'list':{'$in':[2,3,4]}}):
print(row) print('\n------------size=10')
for row in collection.find({'list':{'$size':10}}):
print(row) # print('-------------------$unset')
# print('$unset和$set相反表示移除文档属性')
# print('---before')
# for row in collection.find({'name': "张程芬"}):
# print(row)
# collection.update({'name':'张程芬'},{'$unset':{'age':1}})
# print('---after')
# for row in collection.find({'name':'张程芬'}):
# print(row)

完整代码文件

__author__ = 'Cq'

import pymongo
import random def add_data(collection):
first_name = ["陈","张","李","王","赵"]
second_name = ["冰","鑫","程","爱","暖"]
third_name = ["强","国","明","风","芬"]
data = [
{"_id":int(""+str(i)),
"name":random.choice(first_name)+
random.choice(second_name)+
random.choice(third_name),
"age":random.randint(16,60),
"high":random.randint(170,190),
"list":list(random.randint(1,200) for i in range(10))
} for i in range(5)
]
try:
for record in data:
collection.save(record)
except pymongo.errors.DuplicateKeyError:
print('record exists')
except Exception as e:
print(e) def delete_data(collection):
remove_before = collection.find()
print('---------------delete before--------------------')
for obj in remove_before:
print(obj) collection.delete_many({'age':{'$gt':20,'$lt':30}}) #删除所有满足条件的文档,删除_id大于6,小于100
collection.delete_one({'age':20}) #删除一条满足条件的文档,删除_id=6
#collection_set01.delete_many({}) #删除整个集合
remove_after = collection.find() print('---------------delete after--------------------')
for obj in remove_after:
print(obj) def update_data(collection):
collection.replace_one({'_id': 10000}, {'name': '王宝宝'}) #replace_one用指定的key-value替代原来所有的key-value
collection.update_one({"_id": {'$lt': 10008}}, {'$set': {"age": ""}}) #update_one更新已经对应的key-value,其它不变
collection.update_many({'_id': {'$gt': 10007}}, {'$set': {'age': ''}}) #同上,能够update所有符合匹配条件的文档 def select_data(collection): print('\n------------身高小于180:')
print(type(collection.find({'high':{'$lt':180}})))
for row in collection.find({'high':{'$lt':180}}):
print(row)
print(type(collection.find_one({'high':{'$lt':180}})))
print('use find_one:',collection.find_one({'high':{'$lt':180}})['high'])
print('use find_one:',collection.find_one({'high':{'$lt':180}})) print('\n------------查询特定键')
print('------------查询身高大于170,并只列出_id,high和age字段(使用列表形式_id默认打印出来,可以使用{}忽视_id):')
for row in collection.find({'high':{'$gt':170}},projection=['high','age']):
print(row) print('\n------------skip参数用法')
for row in collection.find({'high':{'$gt':170}},['high','age'],skip=1):
print(row)
for row in collection.find({'high':{'$gt':170}},['high','age']).skip(1):
print(row) print('\n------------limit参数用法')
for row in collection.find({'high':{'$gt':170}},['high','age'],limit=1):
print(row) print('\n------------用{}描述特定键')
for row in collection.find({'high':{'$gt':170}},{'high':1,'age':1,'_id':False}):
print(row) print('\n------------多条件查询')
print(collection.find_one({'high':{'$gt':10},'age':{'$lt':26,'$gt':10}})) # for u in db.users.find({"age":{"$nin":(23, 26, 32)}}):
# print u
# select * from users where age not in (23, 26, 32) print('\n------------count')
print(collection.find({"age":{"$gt":20}}).count()) print('\n------------条件或')
print('大于等于29或者小于23')
for row in collection.find({"$or":[{"age":{"$lte":23}}, {"age":{"$gte":29}}]}):
print(row) print('\n------------exists')
for row in collection.find({'age':{'$exists':True}}):
print('age exists',row) # select * from 集合名 where exists 键1
for row in collection.find({'age':{'$exists':False}}):
print('age not exists',row) print('\n------------正则表达式查询')
print('method 1')
for row in collection.find({'name':{'$regex':r'.*暖.*'}}):
print(row)
print('method 2')
import re
Regex = re.compile(r'.*爱.*',re.IGNORECASE)
for row in collection.find({'name':Regex}):
print(row) print('\n------------使用sort排序(文档中没有排序的字段也会打印出来,表示最小)')
print('------------age 升序')
for row in collection.find().sort([["age",pymongo.ASCENDING]]):
print(row)
print('------------age 降序')
for row in collection.find().sort([("age",-1)]):
print(row)
print('------------age升序,high升序')
for row in collection.find().sort((("age",pymongo.ASCENDING),("high",pymongo.ASCENDING))):
print(row)
print('------------age升序,high降序')
for row in collection.find(sort=[("age",pymongo.ASCENDING),("high",pymongo.ASCENDING)]):
print(row) print('\n------------$all')
for row in collection.find({'list':{'$all':[77,117,165,37,57,49,178,90,3,166]}}):
print(row) print('\n------------$in')
for row in collection.find({'list':{'$in':[2,3,4]}}):
print(row) print('\n------------size=10')
for row in collection.find({'list':{'$size':10}}):
print(row) # print('-------------------$unset')
# print('$unset和$set相反表示移除文档属性')
# print('---before')
# for row in collection.find({'name': "张程芬"}):
# print(row)
# collection.update({'name':'张程芬'},{'$unset':{'age':1}})
# print('---after')
# for row in collection.find({'name':'张程芬'}):
# print(row) def main():
client = pymongo.MongoClient('192.168.198.128', 27017, username='guest', password='') db = client.test collection = db.test add_data(collection) update_data(collection) select_data(collection) delete_data(collection) if "__main__" == __name__:
main()

输出结果

 ------------身高小于180:
<class 'pymongo.cursor.Cursor'>
{'_id': 10002, 'name': '赵爱风', 'age': 24, 'high': 172, 'list': [37, 116, 190, 120, 15, 101, 95, 159, 43, 34]}
{'_id': 10003, 'name': '李爱风', 'age': 31, 'high': 170, 'list': [170, 167, 197, 184, 58, 83, 79, 122, 149, 11]}
<class 'dict'>
use find_one: 172
use find_one: {'_id': 10002, 'name': '赵爱风', 'age': 24, 'high': 172, 'list': [37, 116, 190, 120, 15, 101, 95, 159, 43, 34]} ------------查询特定键
------------查询身高大于170,并只列出_id,high和age字段(使用列表形式_id默认打印出来,可以使用{}忽视_id):
{'_id': 10001, 'age': 21, 'high': 186}
{'_id': 10002, 'age': 24, 'high': 172}
{'_id': 10004, 'age': 41, 'high': 182} ------------skip参数用法
{'_id': 10002, 'age': 24, 'high': 172}
{'_id': 10004, 'age': 41, 'high': 182}
{'_id': 10002, 'age': 24, 'high': 172}
{'_id': 10004, 'age': 41, 'high': 182} ------------limit参数用法
{'_id': 10001, 'age': 21, 'high': 186} ------------用{}描述特定键
{'age': 21, 'high': 186}
{'age': 24, 'high': 172}
{'age': 41, 'high': 182} ------------多条件查询
{'_id': 10001, 'name': '王鑫风', 'age': 21, 'high': 186, 'list': [133, 19, 191, 74, 113, 39, 95, 149, 91, 103]} ------------count
4 ------------条件或
大于等于29或者小于23
{'_id': 10001, 'name': '王鑫风', 'age': 21, 'high': 186, 'list': [133, 19, 191, 74, 113, 39, 95, 149, 91, 103]}
{'_id': 10003, 'name': '李爱风', 'age': 31, 'high': 170, 'list': [170, 167, 197, 184, 58, 83, 79, 122, 149, 11]}
{'_id': 10004, 'name': '李程明', 'age': 41, 'high': 182, 'list': [122, 1, 80, 145, 151, 114, 143, 56, 122, 100]} ------------exists
age exists {'_id': 10000, 'name': '王宝宝', 'age': ''}
age exists {'_id': 10001, 'name': '王鑫风', 'age': 21, 'high': 186, 'list': [133, 19, 191, 74, 113, 39, 95, 149, 91, 103]}
age exists {'_id': 10002, 'name': '赵爱风', 'age': 24, 'high': 172, 'list': [37, 116, 190, 120, 15, 101, 95, 159, 43, 34]}
age exists {'_id': 10003, 'name': '李爱风', 'age': 31, 'high': 170, 'list': [170, 167, 197, 184, 58, 83, 79, 122, 149, 11]}
age exists {'_id': 10004, 'name': '李程明', 'age': 41, 'high': 182, 'list': [122, 1, 80, 145, 151, 114, 143, 56, 122, 100]} ------------正则表达式查询
method 1
method 2
{'_id': 10002, 'name': '赵爱风', 'age': 24, 'high': 172, 'list': [37, 116, 190, 120, 15, 101, 95, 159, 43, 34]}
{'_id': 10003, 'name': '李爱风', 'age': 31, 'high': 170, 'list': [170, 167, 197, 184, 58, 83, 79, 122, 149, 11]} ------------使用sort排序(文档中没有排序的字段也会打印出来,表示最小)
------------age 升序
{'_id': 10001, 'name': '王鑫风', 'age': 21, 'high': 186, 'list': [133, 19, 191, 74, 113, 39, 95, 149, 91, 103]}
{'_id': 10002, 'name': '赵爱风', 'age': 24, 'high': 172, 'list': [37, 116, 190, 120, 15, 101, 95, 159, 43, 34]}
{'_id': 10003, 'name': '李爱风', 'age': 31, 'high': 170, 'list': [170, 167, 197, 184, 58, 83, 79, 122, 149, 11]}
{'_id': 10004, 'name': '李程明', 'age': 41, 'high': 182, 'list': [122, 1, 80, 145, 151, 114, 143, 56, 122, 100]}
{'_id': 10000, 'name': '王宝宝', 'age': ''}
------------age 降序
{'_id': 10000, 'name': '王宝宝', 'age': ''}
{'_id': 10004, 'name': '李程明', 'age': 41, 'high': 182, 'list': [122, 1, 80, 145, 151, 114, 143, 56, 122, 100]}
{'_id': 10003, 'name': '李爱风', 'age': 31, 'high': 170, 'list': [170, 167, 197, 184, 58, 83, 79, 122, 149, 11]}
{'_id': 10002, 'name': '赵爱风', 'age': 24, 'high': 172, 'list': [37, 116, 190, 120, 15, 101, 95, 159, 43, 34]}
{'_id': 10001, 'name': '王鑫风', 'age': 21, 'high': 186, 'list': [133, 19, 191, 74, 113, 39, 95, 149, 91, 103]}
------------age升序,high升序
{'_id': 10001, 'name': '王鑫风', 'age': 21, 'high': 186, 'list': [133, 19, 191, 74, 113, 39, 95, 149, 91, 103]}
{'_id': 10002, 'name': '赵爱风', 'age': 24, 'high': 172, 'list': [37, 116, 190, 120, 15, 101, 95, 159, 43, 34]}
{'_id': 10003, 'name': '李爱风', 'age': 31, 'high': 170, 'list': [170, 167, 197, 184, 58, 83, 79, 122, 149, 11]}
{'_id': 10004, 'name': '李程明', 'age': 41, 'high': 182, 'list': [122, 1, 80, 145, 151, 114, 143, 56, 122, 100]}
{'_id': 10000, 'name': '王宝宝', 'age': ''}
------------age升序,high降序
{'_id': 10001, 'name': '王鑫风', 'age': 21, 'high': 186, 'list': [133, 19, 191, 74, 113, 39, 95, 149, 91, 103]}
{'_id': 10002, 'name': '赵爱风', 'age': 24, 'high': 172, 'list': [37, 116, 190, 120, 15, 101, 95, 159, 43, 34]}
{'_id': 10003, 'name': '李爱风', 'age': 31, 'high': 170, 'list': [170, 167, 197, 184, 58, 83, 79, 122, 149, 11]}
{'_id': 10004, 'name': '李程明', 'age': 41, 'high': 182, 'list': [122, 1, 80, 145, 151, 114, 143, 56, 122, 100]}
{'_id': 10000, 'name': '王宝宝', 'age': ''} ------------$all ------------$in ------------size=10
{'_id': 10001, 'name': '王鑫风', 'age': 21, 'high': 186, 'list': [133, 19, 191, 74, 113, 39, 95, 149, 91, 103]}
{'_id': 10002, 'name': '赵爱风', 'age': 24, 'high': 172, 'list': [37, 116, 190, 120, 15, 101, 95, 159, 43, 34]}
{'_id': 10003, 'name': '李爱风', 'age': 31, 'high': 170, 'list': [170, 167, 197, 184, 58, 83, 79, 122, 149, 11]}
{'_id': 10004, 'name': '李程明', 'age': 41, 'high': 182, 'list': [122, 1, 80, 145, 151, 114, 143, 56, 122, 100]}
---------------delete before--------------------
{'_id': 10000, 'name': '王宝宝', 'age': ''}
{'_id': 10001, 'name': '王鑫风', 'age': 21, 'high': 186, 'list': [133, 19, 191, 74, 113, 39, 95, 149, 91, 103]}
{'_id': 10002, 'name': '赵爱风', 'age': 24, 'high': 172, 'list': [37, 116, 190, 120, 15, 101, 95, 159, 43, 34]}
{'_id': 10003, 'name': '李爱风', 'age': 31, 'high': 170, 'list': [170, 167, 197, 184, 58, 83, 79, 122, 149, 11]}
{'_id': 10004, 'name': '李程明', 'age': 41, 'high': 182, 'list': [122, 1, 80, 145, 151, 114, 143, 56, 122, 100]}
---------------delete after--------------------
{'_id': 10000, 'name': '王宝宝', 'age': ''}
{'_id': 10003, 'name': '李爱风', 'age': 31, 'high': 170, 'list': [170, 167, 197, 184, 58, 83, 79, 122, 149, 11]}
{'_id': 10004, 'name': '李程明', 'age': 41, 'high': 182, 'list': [122, 1, 80, 145, 151, 114, 143, 56, 122, 100]}

参考博客https://www.cnblogs.com/diaosir/p/6507178.html