MongoDB入门---文档查询之$type操作符&limit方法&skip方法&简单排序(sort)操作
上一篇文章呢,已经分享过了一部分查询操作了,这篇文章呢?就来继续分享哈。接下来呢我们直接看MongoDB中的$type操作符哈。它呢是基于BSON类型来检索集合中匹配的数据类型,并且返回结果,在MongoDB中可以使用的数据类型如下:
| 类型 | 数字 | 备注 |
|---|---|---|
| Double | 1 | |
| String | 2 | |
| Object | 3 | |
| Array | 4 | |
| Binary data | 5 | |
| Undefined | 6 | 已废弃。 |
| Object id | 7 | |
| Boolean | 8 | |
| Date | 9 | |
| Null | 10 | |
| Regular Expression | 11 | |
| JavaScript | 13 | |
| Symbol | 14 | |
| JavaScript (with scope) | 15 | |
| 32-bit integer | 16 | |
| Timestamp | 17 | |
| 64-bit integer | 18 | |
| Min key | 255 | Query with -1. |
| Max key | 127 |
接下来,我们直接在luyaran集合中插入数据来进行$type实例:
>db.luyaran.insert({
title: 'PHP 教程',
description: 'PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。',
by: 'luyaran',
url: 'http://www.luyaran.com',
tags: ['php'],
likes: 200
})
>db.luyaran.insert({title: 'Java 教程',
description: 'Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。',
by: 'luyaran',
url: 'http://www.luyaran.com',
tags: ['java'],
likes: 150
})
>db.luyaran.insert({title: 'MongoDB 教程',
description: 'MongoDB 是一个 Nosql 数据库',
by: 'luyaran',
url: 'http://www.luyaran.com',
tags: ['mongodb'],
likes: 100
})
然后呢,我们查看一下数据哈:
> db.luyaran.find()
{ "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP 教程", "description" : "PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "php" ], "likes" : 200 }
{ "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "java" ], "likes" : 150 }
{ "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "mongodb" ], "likes" : 100 }
接下来我们就要使用$type操作符获取luyaran集合中的title为string的数据喽:
db.luyaran.find({"title" : {$type : 2}})
输出的结果为:
{ "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP 教程", "description" : "PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "php" ], "likes" : 200 }
{ "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "java" ], "likes" : 150 }
{ "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "mongodb" ], "likes" : 100 }
好啦,上面的只是一个简单的例子大家有时间可以多试试,在接下来就是limit(读取指定数量)方法了,看语法:
>db.COLLECTION_NAME.find().limit(NUMBER)
来,我们看实例说话,luyaran集合中我们已知有如下数据:
{ "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP 教程", "description" : "PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "php" ], "likes" : 200 }
{ "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "java" ], "likes" : 150 }
{ "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "mongodb" ], "likes" : 100 }
然后我们来展示文档中的两条数据:
> db.luyaran.find({},{"title":1,_id:0}).limit(2)
{ "title" : "PHP 教程" }
{ "title" : "Java 教程" }
> //如果没有limit的话就会全部展示
完事的话,就要到skip(跳过指定数量)方法了,看一下语法:
>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
完事直接看实例哈:
>db.luyaran.find({},{"title":1,_id:0}).limit(1).skip(1)
{ "title" : "Java 教程" }
>
到这里呢就把skip方法介绍的差不多了,再来就是我们比较重要的排序(sort)了,比较实用哈,看一下语法:
>db.COLLECTION_NAME.find().sort({KEY:1})
还是luyaran这个集合中的数据,咱们直接进行likes这个字段的排序,看一下实例哈:
>db.luyaran.find({},{"title":1,_id:0}).sort({"likes":-1})
{ "title" : "PHP 教程" }
{ "title" : "Java 教程" }
{ "title" : "MongoDB 教程" }
>
OK了,到这里呢查询就分享的差不多了哈,再来就是索引什么的了,小弟我会加把劲的,希望各位不要喷我哈,拜谢。。。对了,下面是本人整理的一些查询方式,有sql对照着:
左边是mongodb查询语句,右边是sql语句。对照着用,挺方便。
db.luyaran.find() select * from luyaran
db.luyaran.find({"age" : 27}) select * from luyaran where age = 27
db.luyaran.find({"username" : "luyaran", "age" : 27}) select * from luyaran where "username" = "luyaran" and age = 27
db.luyaran.find({}, {"username" : 1, "email" : 1}) select username, email from luyaran
db.luyaran.find({}, {"username" : 1, "_id" : 0}) // no case // 即时加上了列筛选,_id也会返回;必须显式的阻止_id返回
db.luyaran.find({"age" : {"$gte" : 18, "$lte" : 30}}) select * from luyaran where age >=18 and age <= 30 // $lt(<) $lte(<=) $gt(>) $gte(>=)
db.luyaran.find({"username" : {"$ne" : "joe"}}) select * from luyaran where username <> "joe"
db.luayran.find({"ticket_no" : {"$in" : [725, 542, 390]}}) select * from luyaran where ticket_no in (725, 542, 390)
db.luyaran.find({"ticket_no" : {"$nin" : [725, 542, 390]}}) select * from luyaran where ticket_no not in (725, 542, 390)
db.luyaran.find({"$or" : [{"ticket_no" : 725}, {"winner" : true}]}) select * form luyaran where ticket_no = 725 or winner = true
db.luyaran.find({"id_num" : {"$mod" : [5, 1]}}) select * from luyaran where (id_num mod 5) = 1
db.luyaran.find({"$not": {"age" : 27}}) select * from luyaran where not (age = 27)
db.luyaran.find({"username" : {"$in" : [null], "$exists" : true}}) select * from luyaran where username is null // 如果直接通过find({"username" : null})进行查询,那么连带"没有username"的纪录一并筛选出来
db.luyaran.find({"name" : /luyaran?/i}) // 正则查询,value是符合PCRE的表达式
db.luyaran.find({fruit : {$all : ["apple", "banana"]}}) // 对数组的查询, 字段fruit中,既包含"apple",又包含"banana"的纪录
db.luyaran.find({"fruit.2" : "peach"}) // 对数组的查询, 字段fruit中,第3个(从0开始)元素是peach的纪录
db.luyaran.find({"fruit" : {"$size" : 3}}) // 对数组的查询, 查询数组元素个数是3的记录,$size前面无法和其他的操作符复合使用
db.luyaran.findOne(criteria, {"comments" : {"$slice" : 10}})//对数组的查询,只返回数组comments中的前十条,还可以{"$slice" : -10},{"$slice":[23, 10]}; 分别返回最后10条,和中间10条
db.luyaran.find({"name.first" : "luyaran", "name.last" : "Schmoe"}) // 嵌套查询
db.luyaran.find({"comments" : {"$elemMatch" : {"author" : "luyaran", "score" : {"$gte" : 5}}}}) // 嵌套查询,仅当嵌套的元素是数组时使用,
db.luyaran.find({"$where" : "this.x + this.y == 10"})//复杂的查询,$where当然是非常方便的,但效率低下。对于复杂查询,考虑的顺序应当是正则->MapReduce->$where
db.luyaran.find({"$where" : "function() { return this.x + this.y == 10; }"}) // $where可以支持javascript函数作为查询条件
db.luyaran.find().sort({"x" : 1}).limit(1).skip(10); // 返回第(10, 11]条,按"x"进行排序;三个limit的顺序是任意的,应该尽量避免skip中使用large-number
好啦,到这里今天的分享就差不多了,如果觉得不错,请多多点赞支持哦。
原文链接:https://blog.csdn.net/luyaran/article/details/79722697
MongoDB入门---文档查询之$type操作符&limit方法&skip方法&简单排序(sort)操作的更多相关文章
- MongoDB入门---文档查询操作之条件查询&and查询&or查询
经过前几天的学习之路,今天终于到了重头戏了.那就是文档查询操作.话不多说哈,直接看下语法: db.collection.find(query, projection) query :可选,使用查询操作 ...
- mongoDB的文档查询
1.简单查询: find() 方法以非结构化的方式来显示所有文档. 语法 MongoDB 查询数据的语法格式如下: collection是集合名字,注意应该是当前数据库的集合,collect ...
- MongoDB快速入门学习笔记4 MongoDB的文档查询操作
先把student删除,再重新插入数据 > db.student.drop() true > db.student.insert([{ "_id" : 1, " ...
- MongoDB入门---文档操作之增删改
之前的两篇文章,已经分享过关于MongoDB的集合还有数据库的各种操作,接下来就涉及到最主要的喽,那就是数据方面的操作,在这里叫做文档操作.话不多说,大家来看正文. 首先来看一下它的数据结构: ...
- MongoDB多文档查询
db.getCollection('transactionCompensation').find( { "$and":[ { "status":{ " ...
- SpringMVC MongoDB之“基本文档查询(Query、BasicQuery)”
一.简介 spring Data MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一篇我 ...
- Spring Data MongoDB 三:基本文档查询(Query、BasicQuery)(一)
一.简单介绍 Spring Data MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一 ...
- Spring Data MongoDB 三:基本文档查询(Query、BasicQuery
一.简介 spring Data MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一篇我 ...
- Spring Data MongoDB 五:进阶文档查询(分页、Morphia)(二)
Spring Data MongoDB 三:基本文档查询(Query.BasicQuery)(一) 学习MongoDB 六: MongoDB查询(游标操作.游标信息)(三) 一.简单介绍 Spring ...
随机推荐
- Dell R720上的系统安装问题的解决办法(关于RAID建立磁盘阵列的技术)
摘要:本篇是本人在搭建大数量存储.搜索环境时,对于使用Dell PowerEdge R720 and R720xd作为服务器所遇到的一些问题进行的总结. 开始时,我们使用Dell提供的安装光盘(蓝色) ...
- 详解为什么32位系统只能用4G内存.
本文转自:https://www.cnblogs.com/nvd11/archive/2013/04/02/2996784.html,感谢作者的干货 既然是详解, 就从最基础的讲起了. 1. Bit( ...
- BZOJ3143:[HNOI2013]游走(高斯消元)
Description 一个无向连通图,顶点从1编号到N,边从1编号到M. 小Z在该图上进行随机游走,初始时小Z在1号顶点,每一步小Z以相等的概率随机选 择当前顶点的某条边,沿着这条边走到下一个顶点, ...
- pycharm设置python文件颜色
File->Settings->Editor->Color Scheme->Python
- REG小探
根键名称缩写对照表 常用数据类型
- LayIM.NetClient 组件开发记录
前言 好久没写博客了.前阶段看了下Hangfire组件,后来对其代码比较感兴趣,当时不太了解他如何生成的页面和一些访问请求等.后来看了下源代码,发现原来是 OWIN 在搞怪.于是乎开始深入研究Hang ...
- VC++ MFC工程中中如何将一个工程的资源(如对话框)复制到另外一个工程
问题的提出:在工程1中用到的资源,在工程2中已有现成的.即工程1中要用到的对话框和工程2的完全相同,而工程2中对该对话框的布局已设计好.控件变量都绑定好了.但由于该对话框的控件特别多,如果在工程1中再 ...
- 【题解】洛谷P2679 [NOIP2015TG] 子串(DP+滚动数组)
次元传送门:洛谷P2679 思路 蒟蒻一开始并没有思路而去看了题解 我们发现对于两个字串的位置 我们只需要管他们匹配成功或者匹配失败即可 f[i][j][k] 记录当前 a[i]不论等不等于b[j] ...
- HDU 1250 Hat's Fibonacci(大数相加)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1250 Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Ot ...
- Java关于NIO类的详解
一.IO与NIO的区别: 前提我们先说一说java IO: Java中使用IO(输入输出)来读取和写入,读写设备上的数据.硬盘文件.内存.键盘......,根据数据的走向可分为输入流和输出流,这个走向 ...