MongoDB快速入门学习笔记4 MongoDB的文档查询操作

时间:2022-09-25 21:28:19

先把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快速入门学习笔记4 MongoDB的文档查询操作的更多相关文章

  1. MongoDB快速入门学习笔记3 MongoDB的文档插入操作

    1.文档的数据存储格式为BSON,类似于JSON.MongoDB插入数据时会检验数据中是否有“_id”,如果没有会自动生成.shell操作有insert和save两种方法.当插入一条数据有“_id”值 ...

  2. MongoDB快速入门学习笔记2 MongoDB的概念及简单操作

    1.以下列举普通的关系型数据库和MongoDB数据库简单概念上的区别: 关系型数据库 MongoDB数据库 说明 database database 数据库 table collection 数据库表 ...

  3. MongoDB快速入门学习笔记8 MongoDB的java驱动操作

    import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; import org.bson.D ...

  4. MongoDB快速入门学习笔记7 MongoDB的用户管理操作

    1.修改启动MongoDB时要求用户验证加参数 --auth 即可.现在我们把MongoDB服务删除,再重新添加服务 mongod --dbpath "D:\work\MongoDB\dat ...

  5. MongoDB快速入门学习笔记6 MongoDB的文档删除操作

    db.集合名称.remove({query}, justOne)query:过滤条件,可选justOne:是否只删除查询到的第一条数据,值为true或者1时,只删除一条数据,默认为false,可选. ...

  6. MongoDB快速入门学习笔记5 MongoDB的文档修改操作

    db.集合名称.update({query},{update},upsert, multi})query:过滤条件update:修改内容upsert:如果不存在查询条件查出的记录,是否插入一条数据,默 ...

  7. MongoDB快速入门学习笔记1 windows安装MongoDB

    1.安装MongoDB 从MongoDB官网上下载MongoDB,我下载的版本是64位的3.2.6.下载完以后直接安装,我的安装目录是D:\work\MongoDB. 2.配置MongoDB的环境变量 ...

  8. 【原创】SpringBoot &amp&semi; SpringCloud 快速入门学习笔记&lpar;完整示例&rpar;

    [原创]SpringBoot & SpringCloud 快速入门学习笔记(完整示例) 1月前在系统的学习SpringBoot和SpringCloud,同时整理了快速入门示例,方便能针对每个知 ...

  9. Sass简单、快速上手&lowbar;Sass快速入门学习笔记总结

    Sass是世界上最成熟.稳定和强大的专业级css扩展语言 ,除了Sass是css的一种预处理器语言,类似的语言还有Less,Stylus等. 这篇文章关于Sass快速入门学习笔记. 资源网站大全 ht ...

随机推荐

  1. winform快速开发平台 -&gt&semi; 基础组件之分页控件

    一个项目控件主要由及部分的常用组件,当然本次介绍的是通用分页控件. 处理思想:我们在处理分页过程中主要是针对数据库操作. 一般情况主要是传递一些开始位置,当前页数,和数据总页数以及相关关联的业务逻辑. ...

  2. jquery 扩展插件方法

    分析插件jquery.countdown.js (function($) { $.fn.countdown = function(options) { // default options var d ...

  3. 深入理解 NodeList

    在web前端编程中,我们通常会通过document.getElementsByTagName的方法取出一组相同标签的dom元素,比如: var list = document.getElementsB ...

  4. NYOJ之Fibonacci数

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAskAAAJwCAIAAAD0kmsHAAAgAElEQVR4nO3dvXLbOMM24O8k3PtA3E

  5. poj1328 贪心

    http://http://poj.org/problem?id=1328 神TM贪心. 不懂请自行搜博客. AC代码: #include<cstdio> #include<algo ...

  6. UICollectionView进阶练习

    上一篇中的干货看完,不觉感觉还是有点虚,今天我们来点实的,做了两个小DEMO,源码已放GitHub,主要是针对UICollectionView做了联系.第一个DEMO是针对UICollectionVi ...

  7. Data Science 和 Finance 两个领域的融合是什么样子的?

    Data Science 和 Finance 两个领域的融合是什么样子的? 2015-05-24 董可人 现在大部分人所说的Quant一般是指各大投行里做衍生品定价,信用评估,风险控制之类工作的人,这 ...

  8. wifi&lowbar;uplink脚本分析

    ~ >: vim apps/tools/wifi_uplink  #!/bin/sh # Copyright (C) 2012 GuoWenxue <guowenxue@gmail.com ...

  9. BZOJ 3384&colon; &lbrack;Usaco2004 Nov&rsqb;Apple Catching 接苹果&lpar; dp &rpar;

    dp dp( x , k ) = max( dp( x - 1 , k - 1 ) + *** , dp( x - 1 , k ) + *** ) *** = 0 or 1 ,根据情况 (BZOJ 1 ...

  10. JavaWeb学习总结&lpar;二&rpar;——Tomcat服务器学习和使用&lpar;一&rpar;(转)

    转载自 http://www.cnblogs.com/xdp-gacl/p/3734395.html 一.Tomcat服务器端口的配置 Tomcat的所有配置都放在conf文件夹之中,里面的serve ...