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. Linux查看系统运行情况

    http://elinux.org/Runtime_Memory_Measurement

  2. winform下的简易播放器

    编写这个播放器,遇到很多问题,比如目前只实现了wav音频文件的播放,而对于这个图中中间所标注的按钮 不能实现让其暂停的功能,同时当点击的时候,让其文本变为"▷",对于这部分功能不知 ...

  3. spark scala学习笔记

    搞清楚几个概念: 闭包 柯里化 搭建了intellij idea 的scala 开发环境

  4. java合并pdf

    一.开发准备 下载pdfbox-app-1.7.1.jar包;下载地址:http://download.csdn.net/detail/yanning1314/4852276 二.简单小例子 在开发中 ...

  5. javascript获取childNodes详情,删除空节点

    chidNodes返回的是node的集合, 每个node都包含有nodeType属性. nodeType取值: 元素节点:1 属性节点:2 文本节点:3 注释节点:8     页面上是由无数个节点组成 ...

  6. C语言中函数声明实现的位置

    在学习C语言的时候我遇到了这么个事情,因为之前先学习的C#,在C#编译器中,函数的声明位置不会影响编译的结果,但是在C语言中却发生了错误 先看一段代码: #include <stdio.h> ...

  7. [SE0]简单的搜索引擎原理

    1.简单了解搜索引擎收录的原理  包括baidu. google .yahoo 在内的各大搜索引擎在内基本上搜录网站的原理大致相同(除了国内某些网站 网1新 l 等采取人工登记的办法),搜索引擎都是采 ...

  8. How to make your assembly more secure from referencing by unauthorized bits

    Now the security has a trend to become more and more important in our daily work, hence I did some r ...

  9. Android开发学习路线图

    Android开发学习方法: Android是一个比较庞大的体系,从底层的Linux内核到上层的应用层,各部分的内容跨度也比较大.因此,一个好的学习方法对我们学习Android开发很重要. 在此建议, ...

  10. 【Postgresql】数据库函数

    1.Postgresql查询前几条记录的SQL语句 select * from table where ...... LIMIT N  ; 2.SQL limit integer offset int ...