MongoDB 文档的查询和插入操作
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
Iterate a Cursor in the mongo Shell
MongoDB 文档的查询和插入操作的更多相关文章
- MongoDB文档(二)--查询
(一)查询文档 查询文档可以使用以下方法 # 以非结构化的方式显示所有的文档 db.<collectionName>.find(document) # 以结构化的方式显示所有文档 db.& ...
- MongoDB(四):数据类型、插入文档、查询文档
1. 数据类型 MongoDB支持许多数据类型. 字符串 - 这是用于存储数据的最常用的数据类型.MongoDB中的字符串必须为UTF-8. 整型 - 此类型用于存储数值. 整数可以是32位或64位, ...
- MongoDB的学习--文档的查询
继续关于<MongoDB权威指南>记录,今天的内容是文档的查询~~ MongoDB官网地址:http://www.mongodb.org/ 我使用的是MongoDB 2.4.8 find函 ...
- MongoDB (八) MongoDB 文档操作
一. MongoDB 插入文档 insert() 方法 要插入数据到 MongoDB 集合,需要使用 MongoDB 的 insert() 或 save() 方法. 语法 insert() 命令的基 ...
- MongoDB学习(查找文档和其他数据查找操作)
理解Cursor对象和查询运算符 cursor对象 cursor对象相当于一个指针,可通过迭代它来访问MongdoDB数据库中的一组对象. 在使用 find() 方法查询时,返回的并非实际文档,而是一 ...
- 【三】MongoDB文档的CURD操作
一.插入文档 使用insert方法插入文档到一个集合中,如果集合不存在创建集合,有以下几种方法: db.collection.insertOne({}):(v3.2 new) #插入一个文档到集合中 ...
- mongodb内嵌文档的查询
本文转自:http://blog.163.com/wm_at163/blog/static/1321734902012526103825481/ 1 > db.blog.findOne() { ...
- mongoDB 文档操作_删
mongoDB 文档删除 MySQL对比 mysql delete from table where ... mongo db.collection.deleteOne(query) 删除函数 del ...
- MongoDB文档的基本操作
1. MongoDB的安装方法 (1)下载MongoDB 相应的版本: (2)设置数据文件和日志文件的存放目录: (3)启动MongoDB服务: (4)将MongoDB作为服务启动. 2. Mongo ...
随机推荐
- java合并pdf
一.开发准备 下载pdfbox-app-1.7.1.jar包;下载地址:http://download.csdn.net/detail/yanning1314/4852276 二.简单小例子 在开发中 ...
- 识别快递单号(2) - 加载图片到canvas
传送门: 识别快递单号(1) - 图像处理 转载请注明出处: http://www.cnblogs.com/zaiyuzhong/p/load-image-to-canvas.html 上篇说到我 ...
- Ajax跨域问题的两种解决方法
浏览器不允许Ajax跨站请求,所以存在Ajax跨域问题,目前主要有两种办法解决. 1.在请求页面上使用Access-Control-Allow-Origin标头. 使用如下标头可以接受全部网站请求: ...
- 【java基础学习】-【泛型】
参考以下几位同学的总结来学习: http://www.cnblogs.com/lwbqqyumidi/p/3837629.html#!comments http://www.weixueyuan.ne ...
- js中使用new Date(str)创建时间对象不兼容firefox和ie的解决方式
/** * 解决 ie,火狐浏览器不兼容new Date(s) * @param strDate * 返回 date对象 * add by zyf at 2015年11月5日 */ function ...
- [RxJava^Android]项目经验分享 --- 递归实现
介绍一下业务逻辑:获取接口数据,根据接口内容判断是否需要继续获取数据. 本文使用递归思路,通过RxJava来实现此功能,获取数据的Observable直接用模拟的Observable.just()替代 ...
- 【总结】C# Access 数据库 增删查改 的简单步骤
引用集: using System.Data.OleDb; static string exePath = System.Environment.CurrentDirectory;//本程序所 ...
- javascript中三种典型情况下this的含义
this本意:基于函数的执行环境绑定. 1)一般函数内部,返回的是window(作用域链中的第二层全局作用域) function test() { return this; } alert(test( ...
- 如何查看bash shell 帮助信息?
man bash 查看bash的命令帮助 info bash 查看bash的文档 help 命令显示bash支持的命令: 如果想看某个命令的帮助可以 help 命令.如 help cd 对bash的命 ...
- 第三周作业(一):安装VS以及创建单元测试
安装的时候找的是最新版本的VS2015,因为不想花钱也不想用破解版,所以用社区版本. 下了一个IOS文件,社区版VS2015,个人免费版,强行表示不用盗版来表现自己高尚的情操:D 放入虚拟光驱软件后, ...