转自:http://www.cnblogs.com/yangecnu/archive/2011/07/16/2108450.html

一.通过查询获取数据

在深入讨论查询之前,首先来了解一下查询返回的结果——游标(cursor)对象。上一篇文章中我们使用的是最简单的find() 查询方法,它会返回结果集中的所有对象,稍后将讨论如何查询特定数据集。

为了看到集合中的所用元素,我们需要使用到find ()函数返回的cursor对象。让我们来重复上一篇文章中使用的find()函数,不过这次我们使用的是find()返回的cursor对象,然后使用while循环遍历cursor对象输出:

> var cursor=db.things.find();
> while(cursor.hasNext()) printjson(cursor.next());
{ "_id" : ObjectId("4e205546b3fcd89b00572c31"), "name" : "mongo" }
{ "_id" : ObjectId("4e20554fb3fcd89b00572c32"), "x" : 3 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c33"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c34"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c35"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c36"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c37"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c38"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c39"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3a"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3b"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3c"), "x" : 4, "j" : 10 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3d"), "x" : 4, "j" : 11 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3e"), "x" : 4, "j" : 12 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3f"), "x" : 4, "j" : 13 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c40"), "x" : 4, "j" : 14 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c41"), "x" : 4, "j" : 15 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c42"), "x" : 4, "j" : 16 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c43"), "x" : 4, "j" : 17 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c44"), "x" : 4, "j" : 18 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c45"), "x" : 4, "j" : 19 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c46"), "x" : 4, "j" : 20 }

语法有点像C#,hasNext()函数判断是否有记录。next()函数返回下一条记录。使用内置的printjson()方法能够将结果输出为json格式。

我们也可以直接在返回的cursor对象上使用forEach()函数而不是while循环来达到同样的效果:

> db.things.find().forEach(printjson);
{ "_id" : ObjectId("4e205546b3fcd89b00572c31"), "name" : "mongo" }
{ "_id" : ObjectId("4e20554fb3fcd89b00572c32"), "x" : 3 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c33"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c34"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c35"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c36"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c37"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c38"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c39"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3a"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3b"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3c"), "x" : 4, "j" : 10 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3d"), "x" : 4, "j" : 11 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3e"), "x" : 4, "j" : 12 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3f"), "x" : 4, "j" : 13 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c40"), "x" : 4, "j" : 14 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c41"), "x" : 4, "j" : 15 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c42"), "x" : 4, "j" : 16 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c43"), "x" : 4, "j" : 17 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c44"), "x" : 4, "j" : 18 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c45"), "x" : 4, "j" : 19 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c46"), "x" : 4, "j" : 20 }

使用forEach()我们必须事先定义一个能够从游标中返回每条记录的函数,上面的printjson就是内置的函数。

在Mongodb中也可以像使用数组一样来使用游标。

> var cursor=db.things.find()
> printjson(cursor[4])
{ "_id" : ObjectId("4e205693b3fcd89b00572c35"), "x" : 4, "j" : 3 }

以上命令返回记录集中的第五条数据。可见curosr中下标是从0开始的。

当使用上面语句的时候,从第一条到第四条(cursor[4])的记录会同时被加载到RAM中。对于大量数据机来说,会耗用大量内存,这是不合适的。游标应该能够通过具体的查询语句来返回任意需要的元素。

除了能够以数组的方式访问游标外,也可以将游标转换为真正的数组:

> var arr=db.things.find().toArray();
> arr[5];
{ "_id" : ObjectId("4e205693b3fcd89b00572c36"), "x" : 4, "j" : 4 }

这种以array的形式特定于mongo交互式命令行中使用,并不使用与所有的驱动中。

二.根据查询条件返回特定的记录

前面讲到了如何通过cursor对象返回记录,现在我们来看如何通过定制查询条件返回特定的记录。在mongodb中数据是以键-值对的形式存储的。在下面的例子中,给出了SQL语句和其对应的在MongoDB中查询方式。定制查询条件是MongoDB的基础。

SELECT * FROM things WHERE name="mongo"
> db.things.find({name:"mongo"}).forEach(printjson)
{ "_id" : ObjectId("4e205546b3fcd89b00572c31"), "name" : "mongo" }
SELECT * FROM things WHERE x=4
> db.things.find({x:4}).forEach(printjson)
{ "_id" : ObjectId("4e205693b3fcd89b00572c33"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c34"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c35"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c36"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c37"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c38"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c39"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3a"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3b"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3c"), "x" : 4, "j" : 10 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3d"), "x" : 4, "j" : 11 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3e"), "x" : 4, "j" : 12 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3f"), "x" : 4, "j" : 13 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c40"), "x" : 4, "j" : 14 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c41"), "x" : 4, "j" : 15 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c42"), "x" : 4, "j" : 16 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c43"), "x" : 4, "j" : 17 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c44"), "x" : 4, "j" : 18 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c45"), "x" : 4, "j" : 19 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c46"), "x" : 4, "j" : 20 }

查询表达式中 { a:A, b:B, ... } 和SQL语句中的 "where a==A and b==B and ...”对应。

除了全部返回数据项外,我们还可以指定返回特定的列。

SELECT * FROM things WHERE x=4
> db.things.find({x:4},{j:true}).forEach(printjson)
{ "_id" : ObjectId("4e205693b3fcd89b00572c33"), "j" : 1 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c34"), "j" : 2 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c35"), "j" : 3 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c36"), "j" : 4 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c37"), "j" : 5 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c38"), "j" : 6 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c39"), "j" : 7 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3a"), "j" : 8 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3b"), "j" : 9 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3c"), "j" : 10 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3d"), "j" : 11 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3e"), "j" : 12 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c3f"), "j" : 13 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c40"), "j" : 14 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c41"), "j" : 15 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c42"), "j" : 16 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c43"), "j" : 17 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c44"), "j" : 18 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c45"), "j" : 19 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c46"), "j" : 20 }

从中可以看出_id项是默认总是显示的。

findOne()-语法糖

为了方便,mongo交互式命令提供了findOne函数用来放回一条记录而不是cursor对象。findOne()函数的参数和find()的参数是一样的,与find函数返回cursor对象不同,findOne返回数据库集合中的第一条记录或者是null,

比如要返回一条name为mongo的记录,有很多中方式,比如今调用cursor函数的next()一次,或者是一数组的方式访问cursor并取它的第一个元素。

但是findOne提供的实现这一目的的方法简单且高效。

> printjson(db.things.findOne({name:"mongo"}))
{ "_id" : ObjectId("4e205546b3fcd89b00572c31"), "name" : "mongo" }

这个命令等同于find({name:”mongo”}).limit(1)

也可以通过唯一值_id来返回一条记录、

> var doc = db.things.findOne({_id:ObjectId("4c2209f9f3924d31102bd84a")});
> doc
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }

使用limite函数限制返回的记录

可以使用limite()方法来限制返回结果集的条数。例如返回记录集的前三条记录。

> db.things.find().limit(3)
{ "_id" : ObjectId("4e205546b3fcd89b00572c31"), "name" : "mongo" }
{ "_id" : ObjectId("4e20554fb3fcd89b00572c32"), "x" : 3 }
{ "_id" : ObjectId("4e205693b3fcd89b00572c33"), "x" : 4, "j" : 1 }

三.更多的帮助

如果不知道函数的用法,可以直接输入函数名(不带())来返回函数的详细用法:

> printjson
function (x) {
    print(tojson(x));
}

本文简单介绍了MongoDB的查询,参考的是官方的tutorial,希望对您有帮助。

[转]MongoDB随笔2:使用查询的更多相关文章

  1. MongoDB随笔3:使用索引

    创建索引的语句很简单. 1.单键索引的创建:db.test.ensureIndex({name:1},{name:'index_name'}) 2.复合索引的创建:db.test.ensureInde ...

  2. NodeJs操作MongoDB之多表查询($lookup)与常见问题

    NodeJs操作MongoDB之多表查询($lookup)与常见问题 一,方法介绍 aggregate()方法来对数据进行聚合操作.aggregate()方法的语法如下 1 aggregate(ope ...

  3. mongoDB的文档查询

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

  4. 7. java操作MongoDB,采用_id查询

    转自:https://www.2cto.com/database/201704/633262.html mongodb命令行_id查询方法 直接用ObjectId() db.getCollection ...

  5. MongoDB文档(二)--查询

    (一)查询文档 查询文档可以使用以下方法 # 以非结构化的方式显示所有的文档 db.<collectionName>.find(document) # 以结构化的方式显示所有文档 db.& ...

  6. MongoDB学习笔记六—查询下

    查询内嵌文档 数据准备 > db.blog.find().pretty() { "_id" : ObjectId("585694e4c5b0525a48a441b5 ...

  7. MongoDB学习笔记五—查询上

    数据准备 { , "goods_name" : "KD876", "createTime" : ISODate("2016-12- ...

  8. MongoDB 文档的查询和插入操作

    MongoDB是文档型数据库,有一些专门的术语,和关系型DB相似,但也有差异,例如,Collection类似于关系型DB的Table,document类似于row,key/value pair类似于c ...

  9. Mongodb profile(慢查询日志)

    在MySQL中,慢查询日志是经常作为我们优化数据库的依据,那在MongoDB中是否有类似的功能呢?答案是肯定的,那就是MongoDB Database Profiler.所以MongoDB 不仅有,而 ...

随机推荐

  1. perl模块终极解决方案--转载

    不管别人怎么说,反正我是非常喜欢perl语言的! 也会继续学习,以前写过不少perl模块的博客,发现有点乱,正好最近看到了关于local::lib这个模块. 居然是用来解决没有root权限的用户安装, ...

  2. vim 安装vim-airline

    在.vimrc中添加 Plugin 'vim-airline/vim-airline' Plugin 'vim-airline/vim-airline-themes' 然后打开vim编辑器执行 :Pl ...

  3. vue之双绑实现

    // html <body> <div id="app"> <input type="text" v-model="nu ...

  4. Java实现时钟小程序【代码】

    哎,好久没上博客园发东西了,上一次还是两个月前的五一写的一篇计算器博客,不过意外的是那个程序成了这学期的Java大作业,所以后来稍微改了一下那个程序就交了上去,这还是美滋滋.然后五月中旬的时候写了一个 ...

  5. Curious Array CodeForces - 407C (高阶差分)

    高阶差分板子题 const int N = 1e5+111; int a[N], n, m, k; int C[N][111], d[N][111]; signed main() { scanf(&q ...

  6. 【Demo】CSS3元素旋转、缩放、移动或倾斜

    CSS3元素旋转.缩放.移动或倾斜 代码: <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  7. IOS-网络(NSURLSession)

    一.NSURLSession的基本用法 // // ViewController.m // NSURLSession // // Created by ma c on 16/2/1. // Copyr ...

  8. Fedora BCM43142 无线网卡驱动安装

    OS:Fedora 25 KDE 系统内核:4.10.16-200.fc25.x86_64 #1 网卡:BCM43142 1.识别自己的网卡型号:命令:lspci | grep -i broadcom ...

  9. 一个自动化测试工具 UI Recorder

    链接 教程 UI Recorder 是一款零成本UI自动化录制工具,类似于Selenium IDE. UI Recorder 要比Selenium IDE更加强大! UI Recorder 非常简单易 ...

  10. 转:application/json 四种常见的 POST 提交数据方式

    四种常见的 POST 提交数据方式 HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT.DELETE.TRACE.CONNECT 这几种.其中 PO ...