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

本文涉及的产品
云数据库 MongoDB,独享型 2核8GB
推荐场景:
构建全方位客户视图
简介: MongoDB数据库的增删查改1.插入数据语法:db.集合名称.insert(document)db.table_name.insert({name:'gj',gender:1})db.table_name.

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( ,,{multi: })
参数说明:

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(,{justOne: })
参数说明:

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 }
原文地址https://www.cnblogs.com/renpingsheng/p/10582698.html

相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。   相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
相关文章
|
1月前
|
NoSQL Cloud Native atlas
探索云原生数据库:MongoDB Atlas 的实践与思考
【10月更文挑战第21天】本文探讨了MongoDB Atlas的核心特性、实践应用及对云原生数据库未来的思考。MongoDB Atlas作为MongoDB的云原生版本,提供全球分布式、完全托管、弹性伸缩和安全合规等优势,支持快速部署、数据全球化、自动化运维和灵活定价。文章还讨论了云原生数据库的未来趋势,如架构灵活性、智能化运维和混合云支持,并分享了实施MongoDB Atlas的最佳实践。
|
2月前
|
NoSQL Cloud Native atlas
探索云原生数据库:MongoDB Atlas 的实践与思考
【10月更文挑战第20天】本文探讨了MongoDB Atlas的核心特性、实践应用及对未来云原生数据库的思考。MongoDB Atlas作为云原生数据库服务,具备全球分布、完全托管、弹性伸缩和安全合规等优势,支持快速部署、数据全球化、自动化运维和灵活定价。文章还讨论了实施MongoDB Atlas的最佳实践和职业心得,展望了云原生数据库的发展趋势。
|
2月前
|
存储 NoSQL MongoDB
MongoDB 数据库引用
10月更文挑战第20天
22 1
|
2月前
|
存储 NoSQL MongoDB
基于阿里云数据库MongoDB版,微财数科“又快又稳”服务超7000万客户
选择MongoDB主要基于其灵活的数据模型、高性能、高可用性、可扩展性、安全性和强大的分析能力。
|
2月前
|
存储 NoSQL MongoDB
mongodb的数据库表怎么创建
在此过程中,理解并掌握这些基本操作,是深入探索MongoDB魅力,乃至构建高效数据解决方案的关键所在。通过实践,您将更加深刻地体会到这种随需应变的数据管理模式带来的便利与效率提升。
35 0
|
2月前
|
存储 NoSQL MongoDB
小川科技携手阿里云数据库MongoDB:数据赋能企业构建年轻娱乐生态
基于MongoDB灵活模式的特性,小川实现了功能的快速迭代和上线,而数据库侧无需任何更改
|
2月前
|
运维 NoSQL BI
简道云搭载阿里云MongoDB数据库,帮助数以万计企业重构业务系统
通过与MongoDB和阿里云团队的合作,让简道云少走了弯路,保障了线上服务的长期稳定运行,提高了吞吐效率,并相应降低了线上运行成本
|
JSON NoSQL Java
mongoDB导出数据库所有集合内容到json文件
网上搜了一圈,官方并有提供批量导出所有集合到json文件的方法。有不少脚本可以实现,但是我还是习惯用java,如下 package starcLL.
2222 0
|
2月前
|
存储 关系型数据库 MySQL
一个项目用5款数据库?MySQL、PostgreSQL、ClickHouse、MongoDB区别,适用场景
一个项目用5款数据库?MySQL、PostgreSQL、ClickHouse、MongoDB——特点、性能、扩展性、安全性、适用场景比较
|
3月前
|
存储 NoSQL 关系型数据库
非关系型数据库-MongoDB技术(二)
非关系型数据库-MongoDB技术(二)