上一篇文章呢,已经分享过了一部分查询操作了,这篇文章呢?就来继续分享哈。接下来呢我们直接看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)操作的更多相关文章

  1. MongoDB入门---文档查询操作之条件查询&and查询&or查询

    经过前几天的学习之路,今天终于到了重头戏了.那就是文档查询操作.话不多说哈,直接看下语法: db.collection.find(query, projection) query :可选,使用查询操作 ...

  2. mongoDB的文档查询

    1.简单查询: find() 方法以非结构化的方式来显示所有文档. 语法 MongoDB 查询数据的语法格式如下:      collection是集合名字,注意应该是当前数据库的集合,collect ...

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

    先把student删除,再重新插入数据 > db.student.drop() true > db.student.insert([{ "_id" : 1, " ...

  4. MongoDB入门---文档操作之增删改

    之前的两篇文章,已经分享过关于MongoDB的集合还有数据库的各种操作,接下来就涉及到最主要的喽,那就是数据方面的操作,在这里叫做文档操作.话不多说,大家来看正文.     首先来看一下它的数据结构: ...

  5. MongoDB多文档查询

    db.getCollection('transactionCompensation').find( { "$and":[ { "status":{ " ...

  6. SpringMVC MongoDB之“基本文档查询(Query、BasicQuery)”

    一.简介 spring Data  MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一篇我 ...

  7. Spring Data MongoDB 三:基本文档查询(Query、BasicQuery)(一)

    一.简单介绍 Spring Data  MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一 ...

  8. Spring Data MongoDB 三:基本文档查询(Query、BasicQuery

    一.简介 spring Data  MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一篇我 ...

  9. Spring Data MongoDB 五:进阶文档查询(分页、Morphia)(二)

    Spring Data MongoDB 三:基本文档查询(Query.BasicQuery)(一) 学习MongoDB 六: MongoDB查询(游标操作.游标信息)(三) 一.简单介绍 Spring ...

随机推荐

  1. request.getParameterMap和request.getParameter不一样的显示

    public class KeywordUtil {    /**     * 只提q参数关键字     *     * @param request     * @return 处理后的关键字Str ...

  2. 记一种c++字符串格式化方法

    std::string str_fmt(const char * _Format, ...) { std::string _str; va_list marker = NULL; va_start(m ...

  3. 国外优秀JavaScript资源推荐

    JavaScript的优秀资源          原文链接:http://code.tutsplus.com/articles/resources-for-staying-on-top-of-java ...

  4. 闲来无事,用javascript写了一个简单的轨迹动画

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. BZOJ 2818 GCD 【欧拉函数 || 莫比乌斯反演】

    传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=2818 2818: Gcd Time Limit: 10 Sec  Memory Limit ...

  6. MyBatis(7)高级查询

    本次全部学习内容:MyBatisLearning 高级查询:   对于整体的工程是时候增加一点文件了: 具体用到那个类再去说明类的内容   一对一查询: 1.resultType进行实现: 执行的sq ...

  7. 使用appium在android9.0真机上测试程序时报错command failed shell “ps ‘uiautomator’”的解决办法

    appium目前最新的windows版本是1.4.16,在android9.0真机上测试程序时会报错:command failed shell “ps ‘uiautomator’”. 网上大多数人的解 ...

  8. Node.js http等模块 笔记05

    一.http模块 const http = require('http'); http.createServer((req,res)=>{ //1 设置响应头 res.writeHead(200 ...

  9. C#的常用类

    BitConverter类:用于将源类型转换成字节数组,或者将字节数组转换成目标类型.在解决不同设备之间产生的大小端问题时,经常使用. Convert类:用于基本数据类型(包括Boolean/Byte ...

  10. DQL-排序查询

    三:排序查询 语法: select  列名 from    表名 where  筛选条件 order by  需要排序的列名   asc/desc 特点:不写升序还是降序,默认升序 排序列表 可以是 ...