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 }
MongoDB数据库(二):增删查改的更多相关文章
- MongoDB数据库进阶 --- 增删查改...
注意: monogdb数据在使用之后必须及时 mongodb.close()否则后台崩溃. 在之前的文章中,我已经介绍了什么事MongoDB以及怎么在windows下安装MongoDB等等基本知识. ...
- java实现简单的数据库的增删查改,并布局交互界面
一.系统简介 1.1.简介 本系统提供了学生信息管理中常见的基本功能,主要包括管理员.管理员的主要功能有对学生信息进行增加.删除.修改.查找等操作,对信息进行管理,对信息进行修改.查找等操作 ...
- [课本]JDBC课程6--使用JDBC的DAO模块化--完成数据库的增删查改_工具类JDBCTools四个(Preparedstatement)功能模块的敲定版
(课本P273-任务九) /**DAO: Data Access Object * 为什么用: 实现功能的模块化,更有利于代码的维护和升级 * 是什么: 访问数据信息的类,包含对数据的CRUD(cre ...
- C#实现对mongoDB的简单增删查改
首先添加所需要驱动包(可通过nuget获得) using MongoDB.Bson;using MongoDB.Driver;using MongoDB.Driver.Builders; 一.设置配置 ...
- SSM框架-MyBatis框架数据库的增删查改操作
话不多说,在User.xml文件中主要写一下操作数据库的sql语句,增,删,查,改是最常见的数据库操作 User.xml文件下:
- js实现对数据库的增删查改
1.查询 复制代码 代码如下: <HTML> <HEAD> <TITLE>数据查询</TITLE> <Script > var conn = ...
- android SQLite使用SQLiteOpenHelper类对数据库进行增删查改
一个简单的例子,当点击按钮时进行相应的操作,效果图如下: 项目代码如下: DatabaseHelper类 package com.example.sqlitedatebasetest; import ...
- SQL Server数据库---》增删查改
***数据的插入:(增) insert into 表名(字段列表) values(值列表) 如果不写字段列表就要为表添加全部的列数据 其实into也可以省略 每次只能插入一条数据 1.如果字段可以为n ...
- 用Jmeter实现mysql数据库的增删查改
主要是参考虫师的“使用JMeter创建数据库(Mysql)测试”. 1.打开Jmeter,点击测试计划 链接:https://pan.baidu.com/s/1ZtaZ6IC_0DRjSlXkjslY ...
随机推荐
- Linux学习之路1
root用户 安装初始系统默认没有启动root用户,如下方式启动: l sudo passwd l 连续输入两次密码,启动root用户 l 再登陆 登陆root用户 l su root l ...
- MonkeyRunner 综合实践
综合实践 测试场景 连接设备,自动安装并启动考研帮app 启动后登录账号(账号zxw1234 密码:zxw123456),然后截图并保存到指定文件位置. 思路分析 连接设备 安装app 启动app 输 ...
- Bootstrap 模态框(Modal)插件id冲突
<!DOCTYPE html><html><head> <meta charset="utf-8"> <titl ...
- ezdxf包下autocad的开发
利用 ezdxf 库读写geotiff格式的地形影像方法(InsertRasterToCAD) 包一:dxfgrabber GitHub - mozman/dxfgrabber: Outdated D ...
- IntelliJ IDEA 创建 Maven简单项目
创建简单Maven项目 使用IDEA提供的Maven工具,根据artifact创建简单Maven项目.根据下图操作,创建Maven项目. 使用IDEA提供的Maven工具创建的Maven简单项目目录结 ...
- CF350E 【Wrong Floyd】
Description 给定n个点,m条边,k个标记点,hack掉给出的程序. Solution 先考虑不可能hack掉的情况.当所有点都是标记点的时候肯定不能hack掉,也就是\(n=k\).还有就 ...
- 【一本通1248:Dungeon Master&&洛谷UVA532 Dungeon Master】
若不会广搜转向[广搜] [题目描述] 这题是一个三维的迷宫题目,其中用‘.’表示空地,‘#’表示障碍物,‘S’表示起点,‘E’表示终点,求从起点到终点的最小移动次数,解法和二维的类似,只是在行动时除了 ...
- 详解最大似然估计(MLE)、最大后验概率估计(MAP),以及贝叶斯公式的理解
转载声明:本文为转载文章,发表于nebulaf91的csdn博客.欢迎转载,但请务必保留本信息,注明文章出处. 原文作者: nebulaf91 原文原始地址:http://blog.csdn.net/ ...
- 第七周博客作业<西北师范大学|李晓婷>
1.助教博客链接:https://home.cnblogs.com/u/lxt-/ 2.本周应批作业0,实批作业0. 3.本周小结:本周我们助教开始准备团队项目题目,下周三之前将会进行作业提交.
- 第四周博客作业<西北师范大学|李晓婷>
1.助教博客链接:https://home.cnblogs.com/u/lxt-/ 2.作业要求链接:www.cnblogs.com/nwnu-daizh/p/10487329.html 3.本周点评 ...