• MongoDB数据库(二):增删查改


    MongoDB数据库的增删查改

    1.插入数据

    语法:

    db.集合名称.insert(document)
    db.table_name.insert({name:'gj',gender:1})
    db.table_name.insert({_id:"20170101",name:'gj',gender:1})
    

    插⼊⽂档时, 如果不指定_id参数,MongoDB会为⽂档分配⼀个唯⼀的ObjectId

    使用insert时如果指定的_id在数据表中已经存在,则插入数据失败

    例子:

    > db.test_table01.insert({_id:1000001,name:"xiaobing",age:30})          # 向数据库中插入一条_id为1000001的数据
    WriteResult({ "nInserted" : 1 })
    
    > db.test_table01.insert({_id:1000001,name:"xiaoming",age:40})          # 再次向数据库中插入一条_id为1000001的数据,会提示失败
    WriteResult({
            "nInserted" : 0,
            "writeError" : {
                    "code" : 11000,
                    "errmsg" : "E11000 duplicate key error collection: study_test.test_table01 index: _id_ dup key: { : 1000001.0 }"
            }
    })
    

    2.保存数据

    语法:

    db.集合名称.save(document)
    

    如果⽂档的_id已经存在则修改文档,如果⽂档的_id不存在则添加,类似于Django中的update_or_create

    例子:

    > db.test_table01.find()
    { "_id" : ObjectId("5c939a4f4c9ce97c5b78a0da"), "name" : "xiaowang", "age" : 10 }
    { "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaohong", "age" : 20 }
    { "_id" : 1000001, "name" : "xiaobing", "age" : 30 }
    
    > db.test_table01.save({_id:1000001,name:"xiaoming",age:40})            # 向数据库中保存_id为1000001的数据,会更新已经的数据
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    
    > db.test_table01.find()
    { "_id" : ObjectId("5c939a4f4c9ce97c5b78a0da"), "name" : "xiaowang", "age" : 10 }
    { "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaohong", "age" : 20 }
    { "_id" : 1000001, "name" : "xiaoming", "age" : 40 }
    

    3.简单查询

    语法:

    db.集合名称.find({条件⽂档})                # 查询所有符合条件的文档
    db.集合名称.findOne({条件⽂档})             # 根据条件查询文档,只返回第⼀个
    db.集合名称.find({条件⽂档}).pretty()       # 根据条件查询文档,并将输出结果格式化
    

    4.更新

    语法:

    db.集合名称.update(<query> ,<update>,{multi: <boolean>})
    参数说明:
        query:查询条件
        update:更新操作符
        multi:可选,默认是false,表示只更新找到的第⼀条记录,值为true表示把满⾜query条件的⽂档全部更新
    

    "multi update only works with $ operators":只有替换后的字段前面加上"$"时,才可以一次性更新多条数据

    例子:

    > db.userinfo.find().pretty()                                   # userinfo数据库里有5条数据
    {
            "_id" : ObjectId("5c94f37d25000fc9936a9759"),
            "country" : "china",
            "province" : "sh",
            "userid" : "a"
    }
    {
            "_id" : ObjectId("5c94f38425000fc9936a975a"),
            "country" : "china",
            "province" : "sh",
            "userid" : "b"
    }
    {
            "_id" : ObjectId("5c94f38625000fc9936a975b"),
            "country" : "china",
            "province" : "sh",
            "userid" : "c"
    }
    {
            "_id" : ObjectId("5c94f39325000fc9936a975c"),
            "country" : "china",
            "province" : "bj",
            "userid" : "da"
    }
    {
            "_id" : ObjectId("5c94f39925000fc9936a975d"),
            "country" : "china",
            "province" : "bj",
            "userid" : "fa"
    }
    > db.userinfo.update({country:"china"},{set:{country:"zhongguo"}})      # 把country等于china的整条数据替换掉,只替换一条数据
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    
    > db.userinfo.find()                    # 第一条数据被替换,只有_id字段没有改变
    { "_id" : ObjectId("5c94f37d25000fc9936a9759"), "set" : { "country" : "zhongguo" } }
    { "_id" : ObjectId("5c94f38425000fc9936a975a"), "country" : "china", "province" : "sh", "userid" : "b" }
    { "_id" : ObjectId("5c94f38625000fc9936a975b"), "country" : "china", "province" : "sh", "userid" : "c" }
    { "_id" : ObjectId("5c94f39325000fc9936a975c"), "country" : "china", "province" : "bj", "userid" : "da" }
    { "_id" : ObjectId("5c94f39925000fc9936a975d"), "country" : "china", "province" : "bj", "userid" : "fa" }
    
    > db.userinfo.update({country:"china"},{$set:{country:"zhongguo"}})     # 把country等于china的字段更新为country等于zhongguo,只更新一条数据
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    
    > db.userinfo.find()
    { "_id" : ObjectId("5c94f37d25000fc9936a9759"), "set" : { "country" : "zhongguo" } }
    { "_id" : ObjectId("5c94f38425000fc9936a975a"), "country" : "zhongguo", "province" : "sh", "userid" : "b" }
    { "_id" : ObjectId("5c94f38625000fc9936a975b"), "country" : "china", "province" : "sh", "userid" : "c" }
    { "_id" : ObjectId("5c94f39325000fc9936a975c"), "country" : "china", "province" : "bj", "userid" : "da" }
    { "_id" : ObjectId("5c94f39925000fc9936a975d"), "country" : "china", "province" : "bj", "userid" : "fa" }
    
    > db.userinfo.update({country:"china"},{$set:{country:"zhongguo"}},{multi:true})    # 把country等于china的字段更新为country等于zhongguo,multi的值为true,所以更新所有符合条件的数据
    WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
    
    > db.userinfo.find()
    { "_id" : ObjectId("5c94f37d25000fc9936a9759"), "set" : { "country" : "zhongguo" } }
    { "_id" : ObjectId("5c94f38425000fc9936a975a"), "country" : "zhongguo", "province" : "sh", "userid" : "b" }
    { "_id" : ObjectId("5c94f38625000fc9936a975b"), "country" : "zhongguo", "province" : "sh", "userid" : "c" }
    { "_id" : ObjectId("5c94f39325000fc9936a975c"), "country" : "zhongguo", "province" : "bj", "userid" : "da" }
    { "_id" : ObjectId("5c94f39925000fc9936a975d"), "country" : "zhongguo", "province" : "bj", "userid" : "fa" }
    
    > db.userinfo.update({country:"zhongguo"},{set:{country:"zh_CN"}},{multi:true})     # multi的值为true,但是set前没有加"$",所以此次更新操作没有更新数据
    WriteResult({
            "nMatched" : 0,
            "nUpserted" : 0,
            "nModified" : 0,
            "writeError" : {
                    "code" : 9,
                    "errmsg" : "multi update only works with $ operators"
            }
    })
    
    > db.userinfo.update({country:"zhongguo"},{$set:{country:"zh_CN"}},{multi:true})    # multi的值为true,set前加了"$",所以把所有文档的country字段更新为"zh_CN"
    WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 4 })
    
    > db.userinfo.find()
    { "_id" : ObjectId("5c94f37d25000fc9936a9759"), "set" : { "country" : "zhongguo" } }
    { "_id" : ObjectId("5c94f38425000fc9936a975a"), "country" : "zh_CN", "province" : "sh", "userid" : "b" }
    { "_id" : ObjectId("5c94f38625000fc9936a975b"), "country" : "zh_CN", "province" : "sh", "userid" : "c" }
    { "_id" : ObjectId("5c94f39325000fc9936a975c"), "country" : "zh_CN", "province" : "bj", "userid" : "da" }
    { "_id" : ObjectId("5c94f39925000fc9936a975d"), "country" : "zh_CN", "province" : "bj", "userid" : "fa" }
    

    5.删除文档

    语法:

    db.集合名称.remove(<query>,{justOne: <boolean>})
    参数说明:
        query:可选,删除⽂档的条件
        justOne:可选, 如果设为true或1, 则只删除⼀条, 默认false, 表示删除多条
    

    例子:

    > db.test_table01.find()
    { "_id" : ObjectId("5c939a4f4c9ce97c5b78a0da"), "name" : "xiaoqiang", "age" : 10 }
    { "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
    { "_id" : 1000001, "name" : "xiaobei" }
    { "_id" : ObjectId("5c939dd14c9ce97c5b78a0dc"), "name" : "xiaoqiang", "age" : 20 }
    { "_id" : ObjectId("5c939dd44c9ce97c5b78a0dd"), "name" : "xiaoqiang", "age" : 30 }
    { "_id" : ObjectId("5c939dd74c9ce97c5b78a0de"), "name" : "xiaoqiang", "age" : 40 }
    
    > db.test_table01.remove({name:"xiaobei"})          # 删除所有name为xiaobei的数据
    WriteResult({ "nRemoved" : 1 })     
    
    > db.test_table01.find()
    { "_id" : ObjectId("5c939a4f4c9ce97c5b78a0da"), "name" : "xiaoqiang", "age" : 10 }
    { "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
    { "_id" : ObjectId("5c939dd14c9ce97c5b78a0dc"), "name" : "xiaoqiang", "age" : 20 }
    { "_id" : ObjectId("5c939dd44c9ce97c5b78a0dd"), "name" : "xiaoqiang", "age" : 30 }
    { "_id" : ObjectId("5c939dd74c9ce97c5b78a0de"), "name" : "xiaoqiang", "age" : 40 }
    
    > db.test_table01.remove({name:"xiaoqiang"},{justOne:true})     # 删除一条name等于xiaoqiang的数据
    WriteResult({ "nRemoved" : 1 })
    
    > db.test_table01.find()
    { "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
    { "_id" : ObjectId("5c939dd14c9ce97c5b78a0dc"), "name" : "xiaoqiang", "age" : 20 }
    { "_id" : ObjectId("5c939dd44c9ce97c5b78a0dd"), "name" : "xiaoqiang", "age" : 30 }
    { "_id" : ObjectId("5c939dd74c9ce97c5b78a0de"), "name" : "xiaoqiang", "age" : 40 }
    
    > db.test_table01.remove({name:"xiaoqiang"})         # 删除所有name等于xiaoqiang的数据,共删除了3条数据
    WriteResult({ "nRemoved" : 3 })
    
    > db.test_table01.find()
    { "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
    

    6.⽐较运算符查询

    MongoDB数据库中的⽐较运算符有:

    =                   等于,默认是等于判断,没有运算符
    $lt                 ⼩于
    $lte                ⼩于等于
    $gt                 ⼤于
    $gte                ⼤于等于
    $ne                 不等于
    

    例子:

    > db.test_table01.insert({name:"乔峰",hometown:"丐帮",gender:"male",age:32})            # 向数据表中插入四条数据
    WriteResult({ "nInserted" : 1 })
    > db.test_table01.insert({name:"段誉",hometown:"云南大理",gender:"male",age:22})
    WriteResult({ "nInserted" : 1 })
    > db.test_table01.insert({name:"虚竹",hometown:"少林寺",gender:"male",age:28})
    WriteResult({ "nInserted" : 1 })
    > db.test_table01.insert({name:"王语嫣",hometown:"燕子坞",gender:"female",age:18})
    WriteResult({ "nInserted" : 1 })
    
    > db.test_table01.find()
    { "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
    { "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "乔峰", "hometown" : "丐帮", "gender" : "male", "age" : 32 }
    { "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段誉", "hometown" : "云南大理", "gender" : "male", "age" : 22 }
    { "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虚竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
    { "_id" : ObjectId("5c93a1fd4c9ce97c5b78a0e6"), "name" : "王语嫣", "hometown" : "燕子坞", "gender" : "female", "age" : 18 }
    
    > db.test_table01.find({age:{$lt:20}})                  # 查找年龄小于20岁的所有数据
    { "_id" : ObjectId("5c93a1fd4c9ce97c5b78a0e6"), "name" : "王语嫣", "hometown" : "燕子坞", "gender" : "female", "age" : 18 }
    
    > db.test_table01.find({age:{$lte:25}})                 # 查找年龄小于等于25岁的所有数据
    { "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
    { "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段誉", "hometown" : "云南大理", "gender" : "male", "age" : 22 }
    { "_id" : ObjectId("5c93a1fd4c9ce97c5b78a0e6"), "name" : "王语嫣", "hometown" : "燕子坞", "gender" : "female", "age" : 18 }
    
    > db.test_table01.find({age:{$gt:20}})                  # 查找年龄大于20岁的所有数据
    { "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "乔峰", "hometown" : "丐帮", "gender" : "male", "age" : 32 }
    { "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段誉", "hometown" : "云南大理", "gender" : "male", "age" : 22 }
    { "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虚竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
    
    > db.test_table01.find({age:{$gte:30}})                 # 查找年龄大于等于30岁的所有数据
    { "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "乔峰", "hometown" : "丐帮", "gender" : "male", "age" : 32 }
    
    > db.test_table01.find({age:{$ne:18}})                  # 查找年龄不等于18岁的所有数据
    { "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
    { "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "乔峰", "hometown" : "丐帮", "gender" : "male", "age" : 32 }
    { "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段誉", "hometown" : "云南大理", "gender" : "male", "age" : 22 }
    { "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虚竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
    

    7.逻辑运算符查询

    语法:

    and         条件与,多个条件共同查询
    or          条件或,符合一个条件与可
    

    例子:

    > db.test_table01.find({age:22,gender:"male"})                      # 查找所有年龄为22,性别为男的人
    { "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段誉", "hometown" : "云南大理", "gender" : "male", "age" : 22 }
    
    > db.test_table01.find({age:{$gt:20},gender:"male"})                # 查找所有年龄大于20,性别为男的人
    { "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "乔峰", "hometown" : "丐帮", "gender" : "male", "age" : 32 }
    { "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段誉", "hometown" : "云南大理", "gender" : "male", "age" : 22 }
    { "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虚竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
    
    > db.test_table01.find({$or:[{age:{$gt:20},gender:"male"}]})        # 查找所有年龄大于20或者性别为男的人
    { "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "乔峰", "hometown" : "丐帮", "gender" : "male", "age" : 32 }
    { "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段誉", "hometown" : "云南大理", "gender" : "male", "age" : 22 }
    { "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虚竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
    
    > db.test_table01.find({$or:[{age:{$gte:20},gender:"male"}]})       # 查找所有年龄大于等于20或者性别为男的人
    { "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "乔峰", "hometown" : "丐帮", "gender" : "male", "age" : 32 }
    { "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段誉", "hometown" : "云南大理", "gender" : "male", "age" : 22 }
    { "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虚竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
    

    8.范围运算符查询

    语法:

    $in             在范围之内
    $nin            不在范围之内
    

    例子:

    > db.test_table01.find({age:{$in:[18,28,38]}})          # 查找年龄为18或者28或者38的人
    { "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虚竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
    { "_id" : ObjectId("5c93a1fd4c9ce97c5b78a0e6"), "name" : "王语嫣", "hometown" : "燕子坞", "gender" : "female", "age" : 18 }
    
    > db.test_table01.find({age:{$nin:[18,28,38]}})         # 查找年龄不等于18或者28或者38的人
    { "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
    { "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "乔峰", "hometown" : "丐帮", "gender" : "male", "age" : 32 }
    { "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段誉", "hometown" : "云南大理", "gender" : "male", "age" : 22 }
    

    9.正则表达式语句查询

    语法:

    //              后接正则表达式进行查询
    $regex          后接正则表达式进行查询
    

    例子:

    > db.test_table01.find({name:{$regex:'^王'}})           # 查找姓王的所有人
    { "_id" : ObjectId("5c93a1fd4c9ce97c5b78a0e6"), "name" : "王语嫣", "hometown" : "燕子坞", "gender" : "female", "age" : 18 }
    

    10.limit和skip

    limit()             ⽤于读取指定数量的⽂档
    skip()              ⽤于跳过指定数量的⽂档
    

    语法:

    db.集合名称.find({条件⽂档}).skip(number)               # 按条件查询,跳过number条数据
    db.集合名称.find({条件⽂档}).limit(number)              # 按条件查询,取前number条数据
    db.集合名称.find({条件⽂档}).limit(10).skip(5)          # 按条件查询后,取第6到第10条数据
    db.集合名称.find({条件⽂档}).skip(5).limit(4)           # 按条件查询后,取第6条到和9条数据
    

    例子:

    > db.test_table01.find({gender:"male"})                 # 查询所有的男性
    { "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "乔峰", "hometown" : "丐帮", "gender" : "male", "age" : 32 }
    { "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段誉", "hometown" : "云南大理", "gender" : "male", "age" : 22 }
    { "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虚竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
    
    > db.test_table01.find({gender:"male"}).limit(2)        # 查询所有的男性,取前两条数据
    { "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "乔峰", "hometown" : "丐帮", "gender" : "male", "age" : 32 }
    { "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段誉", "hometown" : "云南大理", "gender" : "male", "age" : 22 }
    
    > db.test_table01.find({gender:"male"}).skip(2)         # 查询所有的男性,跳过前2条数据
    { "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虚竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
    

    11.投影

    语法:

    db.集合名称.find({},{字段名称:1,...})           # 查询结果,只选择必要的字段
    参数为字段与值,值为1表示显示,值为0不显
    

    对于_id列默认是显示的, 如果不显示需要明确设置为0

    例子:

    > db.test_table01.find({gender:"male"},{name:1,hometown:1})         # 查询所有的男性,显示_id,name和hometown
    { "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "乔峰", "hometown" : "丐帮" }
    { "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段誉", "hometown" : "云南大理" }
    { "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虚竹", "hometown" : "少林寺" }
    
    > db.test_table01.find({gender:"male"},{name:1})                    # 查询所有男性,显示name和_id字段
    { "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "乔峰" }
    { "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段誉" }
    { "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虚竹" }
    
    > db.test_table01.find({gender:"male"},{hometown:1})                # 查询所有男性,显示_id和hometown字段
    { "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "hometown" : "丐帮" }
    { "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "hometown" : "云南大理" }
    { "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "hometown" : "少林寺" }
    
    > db.test_table01.find({gender:"male"},{hometown:1,_id:0})          # 查询所有男性,显示hometown,不显示_id字段
    { "hometown" : "丐帮" }
    { "hometown" : "云南大理" }
    { "hometown" : "少林寺" }
    
    > db.test_table01.find({},{name:1,age:1})                           # 查询所有男性,显示_id和name,age字段
    { "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
    { "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "乔峰", "age" : 32 }
    { "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段誉", "age" : 22 }
    { "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虚竹", "age" : 28 }
    { "_id" : ObjectId("5c93a1fd4c9ce97c5b78a0e6"), "name" : "王语嫣", "age" : 18 }
    
    > db.test_table01.find({},{name:1,age:1,_id:0})                     # 查询所有男性,显示name,age字段
    { "name" : "xiaozhao", "age" : 20 }
    { "name" : "乔峰", "age" : 32 }
    { "name" : "段誉", "age" : 22 }
    { "name" : "虚竹", "age" : 28 } 
    { "name" : "王语嫣", "age" : 18 }
    

    12.排序

    sort()          对查询结果进⾏排序
    

    语法:

    db.集合名称.find().sort({字段:1,...})
        参数1为升序排列
        参数-1为降序排列
    

    例子:

    > db.test_table01.find().sort({age:1})                              # 查询所有数据,按age字段升序排列
    { "_id" : ObjectId("5c93a1fd4c9ce97c5b78a0e6"), "name" : "王语嫣", "hometown" : "燕子坞", "gender" : "female", "age" : 18 }
    { "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
    { "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段誉", "hometown" : "云南大理", "gender" : "male", "age" : 22 }
    { "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虚竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
    { "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "乔峰", "hometown" : "丐帮", "gender" : "male", "age" : 32 }
    
    > db.test_table01.find().sort({age:-1})                             # 查询所有数据,按age字段降序排列
    { "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "乔峰", "hometown" : "丐帮", "gender" : "male", "age" : 32 }
    { "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虚竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
    { "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段誉", "hometown" : "云南大理", "gender" : "male", "age" : 22 }
    { "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
    { "_id" : ObjectId("5c93a1fd4c9ce97c5b78a0e6"), "name" : "王语嫣", "hometown" : "燕子坞", "gender" : "female", "age" : 18 }
    
    > db.test_table01.find().sort({name:-1})                            # 查询所有数据,按name字段降序排列
    { "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虚竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
    { "_id" : ObjectId("5c93a1fd4c9ce97c5b78a0e6"), "name" : "王语嫣", "hometown" : "燕子坞", "gender" : "female", "age" : 18 }
    { "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段誉", "hometown" : "云南大理", "gender" : "male", "age" : 22 }
    { "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "乔峰", "hometown" : "丐帮", "gender" : "male", "age" : 32 }
    { "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
    
    > db.test_table01.find().sort({hometown:1})                         # 查询所有数据,按hometown字段升序排列
    { "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
    { "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "乔峰", "hometown" : "丐帮", "gender" : "male", "age" : 32 }
    { "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段誉", "hometown" : "云南大理", "gender" : "male", "age" : 22 }
    { "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虚竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
    { "_id" : ObjectId("5c93a1fd4c9ce97c5b78a0e6"), "name" : "王语嫣", "hometown" : "燕子坞", "gender" : "female", "age" : 18 }
    > db.test_table01.find({age:{$gt:25}}).sort({age:1})                # 查询age大于25的所有数据,按age字段升序排列
    { "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虚竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
    { "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "乔峰", "hometown" : "丐帮", "gender" : "male", "age" : 32 }
    

    13.统计个数

    count()                 统计结果集中⽂档条数
    

    语法:

    db.集合名称.find({查询条件}).count()
    db.集合名称.count({查询条件})
    

    例子:

    > db.test_table01.find({}).count()                      # 查询所有数据的数量
    5       
    > db.test_table01.find({age:{$gt:25}}).count()          # 查询age大于25的所有数据的总数
    2
    > db.test_table01.find({gender:"male"}).count()         # 查询gender等于male的所有数据的总数
    3
    

    14.去重

    distinct()              对数据进⾏去重
    

    语法:

    db.集合名称.distinct('去重字段',{条件})
    

    例子:

    > db.test_table01.distinct("gender")                        # 对gemder字段进行去重
    [ "male", "female" ]
    
    > db.test_table01.distinct("gender",{age:{$gt:20}})         # 对age大于20的所有数据的gender字段信息进行去重
    [ "male" ]
    
    > db.test_table01.distinct("age")                           # 对所有数据的age字段进行去重
    [ 20, 32, 22, 28, 18 ]
    
    > db.test_table01.distinct("name")                          # 对所有数据的name字段进行去重
    [ "xiaozhao", "乔峰", "段誉", "虚竹", "王语嫣" ]
    

    15.⾃定义查询

    语法:

    使⽤$where后⾯写⼀个函数, 返回满⾜条件的数据
    

    例子:

    > db.test_table01.find({$where:function(){return this.age > 30}})       # 查询age大于30的所有数据
    { "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "乔峰", "hometown" : "丐帮", "gender" : "male", "age" : 32 }
  • 相关阅读:
    zigbee芯片
    笔记本ubuntu安装wifi驱动(未完成)
    我错了的N个学习
    《华为工作法》读书笔记
    bbblack的网络socket通信实验
    NB-IOT连接移动onenet平台流程
    移动onenet基础通信套件V1.08版本的AT指令测试
    CC3200使用MQTT的SSL加密证书可用日期修改
    利尔达NB-IOT模块烧写固件的步骤
    树莓派相机
  • 原文地址:https://www.cnblogs.com/renpingsheng/p/10582698.html
Copyright © 2020-2023  润新知