MongoDB是文档型数据库,有一些专门的术语,和关系型DB相似,但也有差异,例如,Collection类似于关系型DB的Table,document类似于row,key/value pair类似于column。document 是使用{}为边界,一个Key/Value对使用“:”分割,key/value pair之间使用“,”分割,例如

user={ name:"sue",age:24 }

MongoDB中能够定义document 数组,这对于批量更新和批量插入操作非常有用。

userArray=
[
  { name:"sue",age:24 },
  { name:"joe",age:25 },
  { name:"pei",age:32 }
]

MongoDB有一个test db,在学习MongoDB时,可以使用use 命令切换到test db。

use test

一,插入操作

MongoDB的插入操作是将document插入到collection中,MongoDB提供三种插入函数db.collection.insert,db.collection.insertOne和db.collection.insertMany,insert函数能够插入单个doc,也能插入doc的数组。

1,插入单个document

user={ name:"test1", age:22}
db.users.insert(user)
db.users.insert( { name:"test1", age:22} )
db.users.insertOne( {name:"test1", age:22} )

2,批量插入document

user1={ name:"t1", age:21}
user2={ name:"t2", age:22}
user3={ name:"t3", age:23} db.users.insert([user1,user2,user3])
db.users.insertMany([user1,user2,user3]) --or
userArray=[user1,user2,user3]
db.users.insertMany([user1,user2,user3])
db.users.insert([user1,user2,user3])

二,查找操作

查询查询的语法

db.collection.find( <query filter>, <projection> )

query filter是查询的过滤条件,projection是从document中查找特定的key/value pair,类似于关系型DB的where子句和select子句。

不加任何query filter时,将查询所有的document

db.users.find()
db.users.find({})

1,Query filter

1.1 在query filter中,“=”使用 key/value pair,或 $eq 表示,两者是等价的

{field: <value>}
{ <field>: { $eq: <value> } }

例如,查询age=21的所有user

db.users.find({age:21})
db.users.find({age:{$eq:21}})

不等式使用$ne表示

{field: {$ne: value} }

例如,查看age<>21的所有user

db.users.find({age:{$ne:21}})

1.2 “>”,“>=”,“<”“<=” 分别使用 $gt,$gte,$lt 和 $lte 表示,格式是

{ field: { $lt: value} }
{ field: { $gt: value} }
{ field: { $lte: value} }
{ field: { $gte: value} }

例如,分别查询age<22 , age>22,age<=22,age>=22的所有user

db.users.find({age:{$lt:22}})
db.users.find({age:{$gt:22}})
db.users.find({age:{$lte:22}})
db.users.find({age:{$gte:22}})

1.3 逻辑或算符使用 $or 表示,格式是

{ $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }

例如,查询age=21或age=22的所有user

db.users.find({$or:[{age:21},{age:22}]})

1.4,逻辑与运算符使用“,” ,或者$and 表示,格式是

{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }

例如,查询name是“t1”,并且 age=21的所有user

db.users.find({$and:[{name:"t1"},{age:21}]})
db.users.find({name:"t1",age:21})

1.5,在范围内查询,使用 $in 表示,不在范围内,使用$nin表示,格式是

{ field: { $in: [<value1>, <value2>, ... <valueN> ] } }
{ field: { $nin: [<value1>, <value2>, ... <valueN> ] } }

例如,查询age是21 或 22的所有user,查询age不是21 和 22的所有user

db.users.find({age:{$in:[21,22]}})
db.users.find({age:{$nin:[21,22]}})

1.6 其他运算符,请参考官方文档《Query and Projection Operators
2,projection

projection是指定collection中的哪些field需要返回,默认情况下,会返回所有的filed。如果没有指定"_id"的projection,那么结果集返回"_id",详细信息参考《Project Fields to Return from Query

a query projection to specifies which fields from the matching documents to return. The projection limits the amount of data that MongoDB returns to the client over the network.

{ field1: <value>, field2: <value> ... }

示例:

db.users.find({$and:[{name:"t1"},{age:21}]},{name:1,_id:0})
db.users.find({$and:[{name:"t1"},{age:21}]},{name:0,_id:0})

3,返回doc 或 iterator

db.collection.find()返回的是cursor,而db.collection.findOne()返回的是single doc。通过db.collection.find() 返回cursor时,必须使用 var 关键字定义cursor,如果没有显式使用var,那么cursor会自动迭代20次,以显示前20个doc。

In the mongo shell, when you assign the cursor returned from the find() method to a variable using the var keyword, the cursor does not automatically iterate. However,if the returned cursor is not assigned to a variable using the var keyword, then the cursor is automatically iterated up to 20 times [1] to print up to the first 20 documents in the results.

step1,使用var 关键字定义cursor

var us =db.users.find()

step2,迭代cursor,将doc以json格式显示

while (us.hasNext())
{
print(tojson(us.next()));
}

step3,使用foreach函数

var us=db.users.find();
us.forEach(printjson);

参考文档:

Query and Projection Operators

MongoDB CRUD Operations

Iterate a Cursor in the mongo Shell

MongoDB 文档的查询和插入操作的更多相关文章

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

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

  2. MongoDB(四):数据类型、插入文档、查询文档

    1. 数据类型 MongoDB支持许多数据类型. 字符串 - 这是用于存储数据的最常用的数据类型.MongoDB中的字符串必须为UTF-8. 整型 - 此类型用于存储数值. 整数可以是32位或64位, ...

  3. MongoDB的学习--文档的查询

    继续关于<MongoDB权威指南>记录,今天的内容是文档的查询~~ MongoDB官网地址:http://www.mongodb.org/ 我使用的是MongoDB 2.4.8 find函 ...

  4. MongoDB (八) MongoDB 文档操作

    一. MongoDB 插入文档 insert() 方法 要插入数据到 MongoDB 集合,需要使用 MongoDB 的  insert() 或 save() 方法. 语法 insert() 命令的基 ...

  5. MongoDB学习(查找文档和其他数据查找操作)

    理解Cursor对象和查询运算符 cursor对象 cursor对象相当于一个指针,可通过迭代它来访问MongdoDB数据库中的一组对象. 在使用 find() 方法查询时,返回的并非实际文档,而是一 ...

  6. 【三】MongoDB文档的CURD操作

    一.插入文档 使用insert方法插入文档到一个集合中,如果集合不存在创建集合,有以下几种方法: db.collection.insertOne({}):(v3.2 new)  #插入一个文档到集合中 ...

  7. mongodb内嵌文档的查询

    本文转自:http://blog.163.com/wm_at163/blog/static/1321734902012526103825481/ 1 > db.blog.findOne() { ...

  8. mongoDB 文档操作_删

    mongoDB 文档删除 MySQL对比 mysql delete from table where ... mongo db.collection.deleteOne(query) 删除函数 del ...

  9. MongoDB文档的基本操作

    1. MongoDB的安装方法 (1)下载MongoDB 相应的版本: (2)设置数据文件和日志文件的存放目录: (3)启动MongoDB服务: (4)将MongoDB作为服务启动. 2. Mongo ...

随机推荐

  1. Uboot+Linux启动时间优化

    动机 设备启动时间往往是项目立项时的一项重要技术指标.快速的启动时间意味着设备宕机时间的缩短,系统的快速恢复,也能改善用户使用时的体验感受,是一项重要的市场竞争力. 准备 优化启动时间之前,我们首先要 ...

  2. Windows服务安装

    运行cmd 输入:cd C:\Windows\Microsoft.NET\Framework\v4.0.30319  按回车 输入安装服务路径:如(installutil.exe D:\Project ...

  3. storm 入门原理介绍

    1.hadoop有master与slave,Storm与之对应的节点是什么? 2.Storm控制节点上面运行一个后台程序被称之为什么? 3.Supervisor的作用是什么? 4.Topology与W ...

  4. (转)Eclipse和MyEclipse安装和使用git(egit)图解笔记

    Eclipse.MyEclipse使用git插件(egit)图解 (转)原文来自:http://www.xuebuyuan.com/446322.html 在开发Java.JavaEE等相关程序时,我 ...

  5. 从Erlang进程看协程思想

    从Erlang进程看协程思想 多核慢慢火了以后,协程类编程也开始越来越火了.比较有代表性的有Go的goroutine.Erlang的Erlang进程.Scala的actor.windows下的fibr ...

  6. CSS3总结

    1.圆角效果 border-radius: 1px 1px 1px 1px; /* 四个半径值分别是左上角.右上角.右下角和左下角.顺时针 */  右边半圆 div.right-circle{ hei ...

  7. Python之路Day12--mysql介绍及操作

    上节回顾: 1. RabbitMQ a. 平均分发 b. perfetch = 1 c. durable 队列持久化  deliver_mode = 2 消息持久化 d. 1对多广播  exchang ...

  8. php中redis的安装

    1.当你在使用php时出现下面的问题 2.通过phpinfo()查看php的版本 我的是php5.6版本 3.查看需要下载的redis的版本 4.点击下面的额链接下载redis:http://wind ...

  9. Lua游戏时区问题

    关于cocos2dx-lua版本中游戏时间显示问题 2015-04-19 19:07 1466人阅读 评论(0) 收藏 举报  分类: Lua(29)   cocos2d(38)  版权声明:本文为博 ...

  10. 关于Map集合

    Map接口实现Collection接口,是集合三大接口之一. Map接口在声明:public interface Map<K,V>;将键映射到值的对象,一个映射不能包含重复的键,每个键最多 ...