MongoDB简介
MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。
MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。
下面给大家介绍MongoDB查询文档操作的实例
先把student删除,再重新插入数据
> db.student.drop() true > db.student.insert([{ "_id" : 1, "name" : "zhangsan", "age": 27, "sex": 1 }, { "_id" : 2, "name" : "lisi", "age": 27 } ,{ "_id" : 3, "name" : "wangwu", "age": 30 }, { "_id" : 4, "name" : "zhaoliu", "age": 28 }, { "_id" : 5, "name" : "qianliu", "age": 33 }, { "_id" : 6, "name" : "sunba", "age": 32 }]) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 6, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) > db.student.find() { "_id" : 1, "name" : "zhangsan", "age" : 27 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 }
1、查询指定键
db.集合名称.find({查询条件},{指定键})
指定键:1表示显示,0表示不显示,_id默认显示
> db.student.find({},{name:1}) { "_id" : 1, "name" : "zhangsan" } { "_id" : 2, "name" : "lisi" } { "_id" : 3, "name" : "wangwu" } { "_id" : 4, "name" : "zhaoliu" } { "_id" : 5, "name" : "qianliu" } { "_id" : 6, "name" : "sunba" } > db.student.find({},{_id:0, age:0}) { "name" : "zhangsan", "sex" : 1 } { "name" : "lisi" } { "name" : "wangwu" } { "name" : "zhaoliu" } { "name" : "qianliu" } { "name" : "sunba" } > db.student.find({},{_id:0, name:1}) { "name" : "zhangsan" } { "name" : "lisi" } { "name" : "wangwu" } { "name" : "zhaoliu" } { "name" : "qianliu" } { "name" : "sunba" }
2、各种查询方式
$lt:< $lte:<= $gt:> $gte:>= $ne:!= > db.student.find({age:{$lt:30}}) { "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } > db.student.find({age:{$ne:27}}) { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 }
$in:包含
$nin:不包含
> db.student.find({age:{$in:[27,28]}}) { "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } > db.student.find({age:{$nin:[27,28]}}) { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 }
$or:或者
> db.student.find({$or:[{age:{$lt:29}}, {name:"sunba"}]}) { "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 6, "name" : "sunba", "age" : 32 }
null:空值
> db.student.find({sex: null}) { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 }
$type:键是某种类型的
double:1
string:2
...
> db.student.insert({_id:7, name:7, age:70}) WriteResult({ "nInserted" : 1 }) > db.student.find({name: {$type: 2}}) { "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 } > db.student.find({name: {$type: 1}}) { "_id" : 7, "name" : 7, "age" : 70 }
正则表达式
> db.student.find({name: /si\b/}) { "_id" : 2, "name" : "lisi", "age" : 27 }
db.集合名称.findOne({查询条件},{指定键})
查询出符合条件的第一条数据
> db.student.findOne() { "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 }
db.集合名称.find({查询条件},{指定键}).limit(数字)
查询前几条数据
> db.student.find().limit(3) { "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 3, "name" : "wangwu", "age" : 30 }
db.集合名称.find({查询条件},{指定键}).skip(数字)
跳过前几条数据
> db.student.find().skip(2) { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 } { "_id" : 7, "name" : 7, "age" : 70 }
可以使用limit()和skip()实现分页
> db.student.find().skip(0).limit(3) { "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 3, "name" : "wangwu", "age" : 30 } > db.student.find().skip(3).limit(3) { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 } > db.student.find().skip(6).limit(3) { "_id" : 7, "name" : 7, "age" : 70 }
db.集合名称.find().sort({键:数字})
数字为1表示升序,数字为2表示降序
> db.student.find().sort({age:1}) { "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 6, "name" : "sunba", "age" : 32 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 7, "name" : 7, "age" : 70 } > db.student.find().sort({age:1, _id:-1}) { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 6, "name" : "sunba", "age" : 32 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 7, "name" : 7, "age" : 70 }
以上内容是小编给大家介绍的MongoDB快速入门笔记(四)之MongoDB查询文档操作实例代码,希望对大家有所帮助!
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]