接着前一篇文章,下面主要介绍一下MongoDB中常用的find操作。

先打开MongoDB shell,通过下面一组命令插入一些数据。

 post1 = {"title":"learn MongoDB", "author":"Wilber", "date":new Date(), "score":}
post2 = {"title":"learn English", "author":"Will", "date":new Date(), "score":}
post3 = {"title":"learn C#", "author":"Li", "date":new Date(), "score":}
post4 = {"title":"learn SQL", "author":"July", "date":new Date(), "score":}
post5 = {"title":"learn Node", "author":"Wilber", "date":new Date(), "score":}
db.blog.posts.insert(post1)
db.blog.posts.insert(post2)
db.blog.posts.insert(post3)
db.blog.posts.insert(post4)
db.blog.posts.insert(post5) users1 = ["Wilber", "Will", "June"]
users2 = ["Will", "July", "Wilber"]
users3 = ["James", "Jack", "Will"]
db.blog.users.insert({"users":users1})
db.blog.users.insert({"users":users2})
db.blog.users.insert({"users":users3})

find(arg1,arg2)简介

find查询会返回一个Collection中文档的子集。关于find的两个参数,arg1表示匹配条件,arg2可以指定要返回的键。

直接上例子,对于arg2,可以通过设置值为0,来控制那些键不要显示

 > db.blog.posts.find({"author":"Will"})
{ "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
29T13::.959Z"), "score" : 95 }
> db.blog.posts.find({}, {"title":, "score":})
{ "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "score" : }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "score" : }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb209"), "title" : "learn C#", "score" : }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb20a"), "title" : "learn SQL", "score" : }
{ "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "score" : }
> db.blog.posts.find({}, {"date": })
{ "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "score" : }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "score" : }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb209"), "title" : "learn C#", "author" : "Li", "score" : }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb20a"), "title" : "learn SQL", "author" : "July", "score" : }
{ "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "score" : }

常用查询条件

比较条件:$gt,$gte,$lt,$lte,$ne

 > db.blog.posts.find({"score":{"$gte":,"$lt":}})
{ "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
-29T13::.939Z"), "score" : 90 }
{ "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
9T13::.999Z"), "score" : 93 }
> db.blog.posts.find({"author":{"$ne":"July"}})
{ "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
-29T13::.939Z"), "score" : 90 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
29T13::.959Z"), "score" : 95 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb209"), "title" : "learn C#", "author" : "Li", "date" : ISODate("2014-11-29T13:2
:.979Z"), "score" : 80 }
{ "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
9T13::.999Z"), "score" : 93 }
>

包含条件:$in,$nin

 > db.blog.posts.find({"author":{"$in":["Wilber","Will"]}})
{ "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
-29T13::.939Z"), "score" : 90 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
29T13::.959Z"), "score" : 95 }
{ "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
9T13::.999Z"), "score" : 93 }
> db.blog.posts.find({"author":{"$nin":["Wilber","Will"]}})
{ "_id" : ObjectId("5479c9f2421b7f1536cfb209"), "title" : "learn C#", "author" : "Li", "date" : ISODate("2014-11-29T13:2
:.979Z"), "score" : 80 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb20a"), "title" : "learn SQL", "author" : "July", "date" : ISODate("2014-11-29T1
::.989Z"), "score" : 70 }
>

或操作:$or

 > db.blog.posts.find({"$or":[{"author":"Wilber"}, {"score":{"$gt":}}]})
{ "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
-29T13::.939Z"), "score" : 90 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
29T13::.959Z"), "score" : 95 }
{ "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
9T13::.999Z"), "score" : 93 }
>

limit(),skip(),sort()

 > db.blog.posts.find().limit()
{ "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
-29T13::.939Z"), "score" : 90 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
29T13::.959Z"), "score" : 95 }
> db.blog.posts.find().skip()
{ "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
29T13::.959Z"), "score" : 95 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb209"), "title" : "learn C#", "author" : "Li", "date" : ISODate("2014-11-29T13:2
:.979Z"), "score" : 80 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb20a"), "title" : "learn SQL", "author" : "July", "date" : ISODate("2014-11-29T1
::.989Z"), "score" : 70 }
{ "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
9T13::.999Z"), "score" : 93 }
> db.blog.posts.find().sort({"athor":,"score":-})
{ "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
29T13::.959Z"), "score" : 95 }
{ "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
9T13::.999Z"), "score" : 93 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
-29T13::.939Z"), "score" : 90 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb209"), "title" : "learn C#", "author" : "Li", "date" : ISODate("2014-11-29T13:2
:.979Z"), "score" : 80 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb20a"), "title" : "learn SQL", "author" : "July", "date" : ISODate("2014-11-29T1
::.989Z"), "score" : 70 }
>

数组的查询

  1. $all:通过多个元素匹配数组
  2. 支持“键.下标”方式的匹配
  3. 支持$size的方式匹配
 > db.blog.users.find({"users":"Will"})
{ "_id" : ObjectId("5479d03d421b7f1536cfb20c"), "users" : [ "Wilber", "Will", "June" ] }
{ "_id" : ObjectId("5479d03d421b7f1536cfb20d"), "users" : [ "Will", "July", "Wilber" ] }
{ "_id" : ObjectId("5479d03d421b7f1536cfb20e"), "users" : [ "James", "Jack", "Will" ] }
> db.blog.users.find({"users": {"$all": ["Wilber", "Will"]}})
{ "_id" : ObjectId("5479d03d421b7f1536cfb20c"), "users" : [ "Wilber", "Will", "June" ] }
{ "_id" : ObjectId("5479d03d421b7f1536cfb20d"), "users" : [ "Will", "July", "Wilber" ] }
> db.blog.users.find({"users": ["Wilber", "Will"]})
> db.blog.users.find({"users.2":"Will"})
{ "_id" : ObjectId("5479d03d421b7f1536cfb20e"), "users" : [ "James", "Jack", "Will" ] }
> db.blog.users.find({"users":{"$size":}})
{ "_id" : ObjectId("5479d03d421b7f1536cfb20c"), "users" : [ "Wilber", "Will", "June" ] }
{ "_id" : ObjectId("5479d03d421b7f1536cfb20d"), "users" : [ "Will", "July", "Wilber" ] }
{ "_id" : ObjectId("5479d03d421b7f1536cfb20e"), "users" : [ "James", "Jack", "Will" ] }
>

null的含义

 > post5.z = null
null
> db.blog.posts.update({"title":"learn Node"}, post5)
> db.blog.posts.find({"z":null})
{ "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
-29T13::.939Z"), "score" : 90 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
29T13::.959Z"), "score" : 95 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb209"), "title" : "learn C#", "author" : "Li", "date" : ISODate("2014-11-29T13:2
:.979Z"), "score" : 80 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb20a"), "title" : "learn SQL", "author" : "July", "date" : ISODate("2014-11-29T1
::.989Z"), "score" : 70 }
{ "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
9T13::.999Z"), "score" : 93, "z" : null }
> db.blog.posts.find({"z":{"$in":[null], "$exists": true}})
{ "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
9T13::.999Z"), "score" : 93, "z" : null }
>

Ps:可以通过以下链接的到例子中的shell命令。

http://files.cnblogs.com/wilber2013/find.js

MongoDB中常用的find的更多相关文章

  1. mongoDB的常用语法

    安装: 到mongodb官网下载安装包或者压缩包:https://www.mongodb.com/download-center?jmp=nav 1.如果是msi包的话则点击按步骤安装,如果是压缩包的 ...

  2. 在MongoDB中实现聚合函数 (转)

    随着组织产生的数据爆炸性增长,从GB到TB,从TB到PB,传统的数据库已经无法通过垂直扩展来管理如此之大数据.传统方法存储和处理数据的成本将会随着数据量增长而显著增加.这使得很多组织都在寻找一种经济的 ...

  3. mongodb的常用操作

    对于nosql之前工作中有用到bekerlydb,最近开始了解mongodb,先简单写下mongodb的一些常用操作,当是个总结: 1.mongodb使用数据库(database)和集合(collec ...

  4. MongoDB实战指南(五):MongoDB中的聚集分析

    聚集操作是对数据进行分析的有效手段.MongoDB主要提供了三种对数据进行分析计算的方式:管道模式聚集分析,MapReduce聚集分析,简单函数和命令的聚集分析. 1. 管道模式进行聚集 这里所说的管 ...

  5. MongoDB中的分组

    一.MongoDB中的Count函数.Distinct函数以及分组 准备工作,插入一个班级的文档 > for(var i=0;i<10;i++){ ... db.Classes.inser ...

  6. 使用highcharts显示mongodb中的数据

    1.mongodb数据表相关 # 显示数据库 show dbs # 数据库 use ceshi # 显示表 show tables # 创建集合 db.createCollection('infoB' ...

  7. 【mongodb系统学习之八】mongodb shell常用操作

    八.mongodb  shell常用基础操作(每个语句后可以加分号,也可以不加,看情况定(有的工具中可以不加),最好是加): 1).进入shell操作界面:mongo,上边已有演示: 2).查看当前使 ...

  8. Mongodb中 Documents文档说明

    mongodb使用BSON格式存储数据记录. 如下图: 文档结构 文档有键值对组成, 有以下结构: {    field1: value1,    field2: value2,    ...     ...

  9. MongoDB的常用命令和增查改删

    数据库操作 Mongodb MySQL 查询库 show databases | show dbs show databases 选中库 use databaseName use databaseNa ...

随机推荐

  1. mysql中varbinary、binary、char、varchar异同

    binary 与 varbinary 类型和char与varchar类型是相似的,只是他们存储的是二进制数据,也就是说他们是包含字节流而不是字符流,他们有二进制字符的集合和顺序,他们的对比,排序是基于 ...

  2. IE兼容性视图设置

    问题: 页面 http://course.upol.cn/lx/jzjjygl/index.html 的课程学习中课程打不开 看了代码是有浏览器版本要求,IE9以上无法访问 解决办法: 1. 在IE设 ...

  3. ES 自动恢复分片的时候不恢复了是磁盘超过了85%,然后不恢复了 ES可以配置多个数据目录

    ES 自动恢复分片的时候不恢复了是磁盘超过了85%,然后不恢复了   ES可以配置多个数据目录

  4. PCL中分割方法的介绍(2)

    (2)关于上一篇博文中提到的欧几里德分割法称之为标准的距离分离,当然接下来介绍其他的与之相关的延伸出来的聚类的方法,我称之为条件欧几里德聚类法,(是我的个人理解),这个条件的设置是可以由我们自定义的, ...

  5. Java知多少(17)强调一下编程风格

    讲完了Java的基础语法,大家就可以编写简单的程序代码了,这里有必要强调一下编程风格. 代码风格虽然不影响程序的运行,但对程序的可读性却非常重要.自己编写的程序要让别人看懂,首先在排版方面要非常注意. ...

  6. Genymotion 解决虚拟镜像下载速度特别慢的问题[转]

    Genymotion下载地址(需注册账号):https://www.genymotion.com/download/ Genymotion号称Android模拟器中运行最快的,但是服务器在国外,And ...

  7. 关于div一侧固定,另一侧自适应

    关于div一侧固定,另一侧自适应,从文字看出 一侧固定:说明有固定长度, 一侧自适应:说明是按比例缩放 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...

  8. R语言合并data.frame

    Merging Data Adding Columns To merge two data frames (datasets) horizontally,  use the merge functio ...

  9. 使用VMware将Linux装在物理硬盘上,开机即可进入Linux (转)

    目录(?)[-] 本文目的 具体操作 1 软件准备 2  安装 21 对硬盘操作 22 创建虚拟机并安装 23 使用Grub引导Linux 1. 本文目的 适合于没有光驱的计算机来安装Linux,还有 ...

  10. (笔记)Linux下C语言实现静态IP地址,掩码,网关的设置

    #include <sys/ioctl.h> #include <sys/types.h> #include <sys/socket.h> #include < ...