MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统
MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案
MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组
下载安装
1 curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz # 下载
2 tar -zxvf mongodb-linux-x86_64-3.0.6.tgz # 解压
3 mv mongodb-linux-x86_64-3.0.6/ /usr/local/mongodb # 将解压包拷贝到指定目录
4 export PATH=<mongodb-install-directory>/bin:$PATH #<mongodb-install-directory> 为Mongo的安装路径,如本文的 /usr/local/mongodb
5 mkdir -p /data/db #创建数据库目录(启动指定--dbpath)
配置文件
1 mongod -f MongoDB.conf 指定配置文件(默认在/etc下寻找)
2
3 基本配置
4 systemLog:
5 destination: file
6 path: /usr/local/var/log/mongodb/mongo.log
7 logAppend: true
8 storage:
9 dbPath: /usr/local/var/mongodb
10 net:
11 bindIp: 127.0.0.1
12 port: 11811
配置文件参数信息
1 #数据库数据存放目录
2 dbpath=/usr/local/mongodb304/data
3 #数据库日志存放目录
4 logpath=/usr/local/mongodb304/logs/mongodb.log
5 #以追加的方式记录日志
6 logappend = true
7 #端口号 默认为27017
8 port=27017
9 #以后台方式运行进程
10 fork=true
11 #开启用户认证
12 auth=true
13 #关闭http接口,默认关闭http端口访问
14 nohttpinterface=true
15 #mongodb所绑定的ip地址
16 bind_ip = 127.0.0.1
17 #启用日志文件,默认启用
18 journal=true
19 #这个选项可以过滤掉一些无用的日志信息,若需要调试使用请设置为false
20 quiet=true
21
22
23 其他配置参数含义
24
25 --quiet # 安静输出
26 --port arg # 指定服务端口号,默认端口27017
27 --bind_ip arg # 绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定默认本地所有IP
28 --logpath arg # 指定MongoDB日志文件,注意是指定文件不是目录
29 --logappend # 使用追加的方式写日志
30 --pidfilepath arg # PID File 的完整路径,如果没有设置,则没有PID文件
31 --keyFile arg # 集群的私钥的完整路径,只对于Replica Set 架构有效
32 --unixSocketPrefix arg # UNIX域套接字替代目录,(默认为 /tmp)
33 --fork # 以守护进程的方式运行MongoDB,创建服务器进程
34 --auth # 启用验证
35 --cpu # 定期显示CPU的CPU利用率和iowait
36 --dbpath arg # 指定数据库路径
37 --diaglog arg # diaglog选项 0=off 1=W 2=R 3=both 7=W+some reads
38 --directoryperdb # 设置每个数据库将被保存在一个单独的目录
39 --journal # 启用日志选项,MongoDB的数据操作将会写入到journal文件夹的文件里
40 --journalOptions arg # 启用日志诊断选项
41 --ipv6 # 启用IPv6选项
42 --jsonp # 允许JSONP形式通过HTTP访问(有安全影响)
43 --maxConns arg # 最大同时连接数 默认2000
44 --noauth # 不启用验证
45 --nohttpinterface # 关闭http接口,默认关闭27018端口访问
46 --noprealloc # 禁用数据文件预分配(往往影响性能)
47 --noscripting # 禁用脚本引擎
48 --notablescan # 不允许表扫描
49 --nounixsocket # 禁用Unix套接字监听
50 --nssize arg (=16) # 设置信数据库.ns文件大小(MB)
51 --objcheck # 在收到客户数据,检查的有效性,
52 --profile arg # 档案参数 0=off 1=slow, 2=all
53 --quota # 限制每个数据库的文件数,设置默认为8
54 --quotaFiles arg # number of files allower per db, requires --quota
55 --rest # 开启简单的rest API
56 --repair # 修复所有数据库run repair on all dbs
57 --repairpath arg # 修复库生成的文件的目录,默认为目录名称dbpath
58 --slowms arg (=100) # value of slow for profile and console log
59 --smallfiles # 使用较小的默认文件
60 --syncdelay arg (=60) # 数据写入磁盘的时间秒数(0=never,不推荐)
61 --sysinfo # 打印一些诊断系统信息
62 --upgrade # 如果需要升级数据库
63
64
65 主/从参数
66 -------------------------------------------------------------------------
67 --master # 主库模式
68 --slave # 从库模式
69 --source arg # 从库 端口号
70 --only arg # 指定单一的数据库复制
71 --slavedelay arg # 设置从库同步主库的延迟时间
72
73
74 Replicaton 参数
75 --------------------------------------------------------------------------------
76 --fastsync # 从一个dbpath里启用从库复制服务,该dbpath的数据库是主库的快照,可用于快速启用同步
77 --autoresync # 如果从库与主库同步数据差得多,自动重新同步,
78 --oplogSize arg # 设置oplog的大小(MB)
View Code
启动mongodb
1 ./mongod --dbpath=/data/db -f MongoDB.conf --rest
2 # 默认端口为:27017
3 # MongoDB 提供了简单的 HTTP 用户界面。 如果你想启用该功能,需要在启动的时候指定参数 --rest
4 # MongoDB 的 Web 界面访问端口比服务的端口多1000。如果你的#MongoDB运行端口使用默认的27017,你可以在端口号为28017访问web用户界面,即地址为:http://localhost:28017
连接mongodb
1 # sudo mongo
2 # sudo mongo --port 11811
3 # sudo mongo -u root -p pwd 127.0.0.1:11811/test
创建管理员
1 > use admin
2 switched to db admin
3 > db
4 admin
5 > db.createUser({user:'admin',pwd:'123456',roles:[{role:'userAdminAnyDatabase',db:'admin'}]})
6 Successfully added user: {
7 "user" : "admin",
8 "roles" : [
9 {
10 "role" : "userAdminAnyDatabase",
11 "db" : "admin"
12 }
13 ]
14 }
15 > exit
创建普通用户
1 > use mydb
2 switched to db mydb
3 > db.createUser({user:'guest',pwd:'123456',roles:[{role:'readWrite',db:'mydb'}]})
4 Successfully added user: {
5 "user" : "guest",
6 "roles" : [
7 {
8 "role" : "readWrite",
9 "db" : "mydb"
10 }
11 ]
12 }
13 > db.auth('guest','123456')
14 1
删除用户
1 > db.dropUser("guest")
2 true
查看存在用户
1 > db.system.users.find()
2 { "_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" } ] }
3 { "_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" } ] }
4 { "_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认证后才能管理其他数据库
View Code
数据类型
数据类型 描述
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 正则表达式类型。用于存储正则表达式。
View Code
Python操作Mongodb模块
1 pip install pymongo
2 or
3 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("1000"+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": "19"}}) #update_one更新已经对应的key-value,其它不变
collection.update_many({'_id': {'$gt': 10007}}, {'$set': {'age': '50'}}) #同上,能够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("1000"+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": "19"}}) #update_one更新已经对应的key-value,其它不变
collection.update_many({'_id': {'$gt': 10007}}, {'$set': {'age': '50'}}) #同上,能够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='123456')
db = client.test
collection = db.test
add_data(collection)
update_data(collection)
select_data(collection)
delete_data(collection)
if "__main__" == __name__:
main()
View Code
输出结果
1 ------------身高小于180:
2 <class 'pymongo.cursor.Cursor'>
3 {'_id': 10002, 'name': '赵爱风', 'age': 24, 'high': 172, 'list': [37, 116, 190, 120, 15, 101, 95, 159, 43, 34]}
4 {'_id': 10003, 'name': '李爱风', 'age': 31, 'high': 170, 'list': [170, 167, 197, 184, 58, 83, 79, 122, 149, 11]}
5 <class 'dict'>
6 use find_one: 172
7 use find_one: {'_id': 10002, 'name': '赵爱风', 'age': 24, 'high': 172, 'list': [37, 116, 190, 120, 15, 101, 95, 159, 43, 34]}
8
9 ------------查询特定键
10 ------------查询身高大于170,并只列出_id,high和age字段(使用列表形式_id默认打印出来,可以使用{}忽视_id):
11 {'_id': 10001, 'age': 21, 'high': 186}
12 {'_id': 10002, 'age': 24, 'high': 172}
13 {'_id': 10004, 'age': 41, 'high': 182}
14
15 ------------skip参数用法
16 {'_id': 10002, 'age': 24, 'high': 172}
17 {'_id': 10004, 'age': 41, 'high': 182}
18 {'_id': 10002, 'age': 24, 'high': 172}
19 {'_id': 10004, 'age': 41, 'high': 182}
20
21 ------------limit参数用法
22 {'_id': 10001, 'age': 21, 'high': 186}
23
24 ------------用{}描述特定键
25 {'age': 21, 'high': 186}
26 {'age': 24, 'high': 172}
27 {'age': 41, 'high': 182}
28
29 ------------多条件查询
30 {'_id': 10001, 'name': '王鑫风', 'age': 21, 'high': 186, 'list': [133, 19, 191, 74, 113, 39, 95, 149, 91, 103]}
31
32 ------------count
33 4
34
35 ------------条件或
36 大于等于29或者小于23
37 {'_id': 10001, 'name': '王鑫风', 'age': 21, 'high': 186, 'list': [133, 19, 191, 74, 113, 39, 95, 149, 91, 103]}
38 {'_id': 10003, 'name': '李爱风', 'age': 31, 'high': 170, 'list': [170, 167, 197, 184, 58, 83, 79, 122, 149, 11]}
39 {'_id': 10004, 'name': '李程明', 'age': 41, 'high': 182, 'list': [122, 1, 80, 145, 151, 114, 143, 56, 122, 100]}
40
41 ------------exists
42 age exists {'_id': 10000, 'name': '王宝宝', 'age': '19'}
43 age exists {'_id': 10001, 'name': '王鑫风', 'age': 21, 'high': 186, 'list': [133, 19, 191, 74, 113, 39, 95, 149, 91, 103]}
44 age exists {'_id': 10002, 'name': '赵爱风', 'age': 24, 'high': 172, 'list': [37, 116, 190, 120, 15, 101, 95, 159, 43, 34]}
45 age exists {'_id': 10003, 'name': '李爱风', 'age': 31, 'high': 170, 'list': [170, 167, 197, 184, 58, 83, 79, 122, 149, 11]}
46 age exists {'_id': 10004, 'name': '李程明', 'age': 41, 'high': 182, 'list': [122, 1, 80, 145, 151, 114, 143, 56, 122, 100]}
47
48 ------------正则表达式查询
49 method 1
50 method 2
51 {'_id': 10002, 'name': '赵爱风', 'age': 24, 'high': 172, 'list': [37, 116, 190, 120, 15, 101, 95, 159, 43, 34]}
52 {'_id': 10003, 'name': '李爱风', 'age': 31, 'high': 170, 'list': [170, 167, 197, 184, 58, 83, 79, 122, 149, 11]}
53
54 ------------使用sort排序(文档中没有排序的字段也会打印出来,表示最小)
55 ------------age 升序
56 {'_id': 10001, 'name': '王鑫风', 'age': 21, 'high': 186, 'list': [133, 19, 191, 74, 113, 39, 95, 149, 91, 103]}
57 {'_id': 10002, 'name': '赵爱风', 'age': 24, 'high': 172, 'list': [37, 116, 190, 120, 15, 101, 95, 159, 43, 34]}
58 {'_id': 10003, 'name': '李爱风', 'age': 31, 'high': 170, 'list': [170, 167, 197, 184, 58, 83, 79, 122, 149, 11]}
59 {'_id': 10004, 'name': '李程明', 'age': 41, 'high': 182, 'list': [122, 1, 80, 145, 151, 114, 143, 56, 122, 100]}
60 {'_id': 10000, 'name': '王宝宝', 'age': '19'}
61 ------------age 降序
62 {'_id': 10000, 'name': '王宝宝', 'age': '19'}
63 {'_id': 10004, 'name': '李程明', 'age': 41, 'high': 182, 'list': [122, 1, 80, 145, 151, 114, 143, 56, 122, 100]}
64 {'_id': 10003, 'name': '李爱风', 'age': 31, 'high': 170, 'list': [170, 167, 197, 184, 58, 83, 79, 122, 149, 11]}
65 {'_id': 10002, 'name': '赵爱风', 'age': 24, 'high': 172, 'list': [37, 116, 190, 120, 15, 101, 95, 159, 43, 34]}
66 {'_id': 10001, 'name': '王鑫风', 'age': 21, 'high': 186, 'list': [133, 19, 191, 74, 113, 39, 95, 149, 91, 103]}
67 ------------age升序,high升序
68 {'_id': 10001, 'name': '王鑫风', 'age': 21, 'high': 186, 'list': [133, 19, 191, 74, 113, 39, 95, 149, 91, 103]}
69 {'_id': 10002, 'name': '赵爱风', 'age': 24, 'high': 172, 'list': [37, 116, 190, 120, 15, 101, 95, 159, 43, 34]}
70 {'_id': 10003, 'name': '李爱风', 'age': 31, 'high': 170, 'list': [170, 167, 197, 184, 58, 83, 79, 122, 149, 11]}
71 {'_id': 10004, 'name': '李程明', 'age': 41, 'high': 182, 'list': [122, 1, 80, 145, 151, 114, 143, 56, 122, 100]}
72 {'_id': 10000, 'name': '王宝宝', 'age': '19'}
73 ------------age升序,high降序
74 {'_id': 10001, 'name': '王鑫风', 'age': 21, 'high': 186, 'list': [133, 19, 191, 74, 113, 39, 95, 149, 91, 103]}
75 {'_id': 10002, 'name': '赵爱风', 'age': 24, 'high': 172, 'list': [37, 116, 190, 120, 15, 101, 95, 159, 43, 34]}
76 {'_id': 10003, 'name': '李爱风', 'age': 31, 'high': 170, 'list': [170, 167, 197, 184, 58, 83, 79, 122, 149, 11]}
77 {'_id': 10004, 'name': '李程明', 'age': 41, 'high': 182, 'list': [122, 1, 80, 145, 151, 114, 143, 56, 122, 100]}
78 {'_id': 10000, 'name': '王宝宝', 'age': '19'}
79
80 ------------$all
81
82 ------------$in
83
84 ------------size=10
85 {'_id': 10001, 'name': '王鑫风', 'age': 21, 'high': 186, 'list': [133, 19, 191, 74, 113, 39, 95, 149, 91, 103]}
86 {'_id': 10002, 'name': '赵爱风', 'age': 24, 'high': 172, 'list': [37, 116, 190, 120, 15, 101, 95, 159, 43, 34]}
87 {'_id': 10003, 'name': '李爱风', 'age': 31, 'high': 170, 'list': [170, 167, 197, 184, 58, 83, 79, 122, 149, 11]}
88 {'_id': 10004, 'name': '李程明', 'age': 41, 'high': 182, 'list': [122, 1, 80, 145, 151, 114, 143, 56, 122, 100]}
89 ---------------delete before--------------------
90 {'_id': 10000, 'name': '王宝宝', 'age': '19'}
91 {'_id': 10001, 'name': '王鑫风', 'age': 21, 'high': 186, 'list': [133, 19, 191, 74, 113, 39, 95, 149, 91, 103]}
92 {'_id': 10002, 'name': '赵爱风', 'age': 24, 'high': 172, 'list': [37, 116, 190, 120, 15, 101, 95, 159, 43, 34]}
93 {'_id': 10003, 'name': '李爱风', 'age': 31, 'high': 170, 'list': [170, 167, 197, 184, 58, 83, 79, 122, 149, 11]}
94 {'_id': 10004, 'name': '李程明', 'age': 41, 'high': 182, 'list': [122, 1, 80, 145, 151, 114, 143, 56, 122, 100]}
95 ---------------delete after--------------------
96 {'_id': 10000, 'name': '王宝宝', 'age': '19'}
97 {'_id': 10003, 'name': '李爱风', 'age': 31, 'high': 170, 'list': [170, 167, 197, 184, 58, 83, 79, 122, 149, 11]}
98 {'_id': 10004, 'name': '李程明', 'age': 41, 'high': 182, 'list': [122, 1, 80, 145, 151, 114, 143, 56, 122, 100]}
View Code