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查询文档操作实例代码,希望对大家有所帮助!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
- 魔兽世界wlk暗牧一键输出宏是什么 wlk暗牧一键输出宏介绍
- 群星.1996-红不让台语原唱2辑【福和唱片】【WAV+CUE】
- 郭书瑶.2009-爱的抱抱(EP)【种子音乐】【FLAC分轨】
- 郑瑞芬.1989-BE.MY.BABY【现代】【WAV+CUE】
- 花钱请人每周放30万只不咬人的蚊子 防治登革热传播
- 饭制《第一后裔》丧尸版弗蕾娜
- 贝克汉姆亲临!2024FC品类游戏嘉年华圆满落幕
- 「命轨爻错之翼」风之翼发放说明
- 《原神》前瞻特别节目回顾长图
- 米游币抽抽乐-原神专场现已开启!
- 黑鸭子2001《风情中国HQCD》[日本版][WAV+CUE]
- 陈杰洲1990-成人礼[滚石][WAV+CUE]
- MarkAanderud-HandsFree(2024)[24-44,1]FLAC
- 孙露《观心》1:1母盘直刻限量版[低速原抓WAV+CUE][361M]
- 钟志刚《汽车DJ玩主》[低速原抓WAV+CUE][1G]