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. Java RMI之HelloWorld篇

    Java RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法.可以用此方 ...

  2. 【SAP BO】无法识别账户信息:无法访问CMS。计算机上的CMS由于某个严重错误而停止。(FWM 20031)

    1.系统环境 OS:Windows Server 2008 R2 RDBMS:Oracle 11g R2(Server.Client同时存在) BI:SAP Business Objects 4.2 ...

  3. 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划

    [BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...

  4. 利用Simple-RTMP-Server(SRS)来进行直播

    1.下载SRS 官方地址:http://www.ossrs.net/srs.release/releases/ 百度地址:http://pan.baidu.com/s/1kV8WQpx 2.编译安装S ...

  5. ACM :漫漫上学路 -DP -水题

    CSU 1772 漫漫上学路 Time Limit: 1000MS   Memory Limit: 131072KB   64bit IO Format: %lld & %llu Submit ...

  6. Python 键盘记录

    之前写的键盘记录最后一直在纠结弹框与不弹框的问题,代码找不到了,今天重新来一遍 #!/usr/bin/env python# -*-coding:utf-8 -*-from ctypes import ...

  7. 在linux中设置静态ip地址

    在linux中设置静态ip地址1.在终端中输入:vi /etc/sysconfig/network-scripts/ifcfg-eth0 2.开始编辑,填写ip地址.子网掩码.网关.DNS等[root ...

  8. 【Beta】Daily Scrum Meeting第二次

    1.任务进度 学号 已完成 接下去要做 502 系负责人及所负责专业的表 写出PHP测试的demo:将okHttp的请求放在非UI线程中执行 509 PHP更该用户信息:更新系负责人所负责系:删除任务 ...

  9. jasmine入门

    本文来自http://blog.fens.me/nodejs-jasmine-bdd 粉丝日志 张丹   前言TDD(Test Driven Development)测试驱动开发,是敏捷开发中提出的最 ...

  10. IDEA插件

    Key Promoter 快捷键提示插件,帮助你快速记住快捷键.当你用鼠标完成某功能时,它会指示有相应的快捷键来完成刚才的功能,同时指导你为经常重复的操作建立快捷键. SerialVersionUID ...