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 ...
随机推荐
- phoneGap
1.安装参考http://blog.csdn.net/mage694/article/details/16846331 2.API 参考http://phonegap.com/developer/
- warning C4996: 'sprintf': This function or variable may be unsafe
选项Project | Configuration Properties | C/C++ | Preprocessor | Preprocessor Defin ...
- CSS常用布局学习笔记
水平居中-行内元素 如果是文字和图片这类的行内元素,则在其父级元素上加上样式:text-align:center; 水平居中-定宽块元素 div{ width:100px; margin:0 auto ...
- [转]字符型IP地址转换成数字IP的SQL函数
使用SQL函数可以实现许多的功能,下面为您介绍的是字符型IP地址转换成数字IP的SQL函数示例,供您参考,希望对您学习SQL函数能够有所帮助. /**//*--调用示例 sele ...
- 初识linux
1.版本 稳定版本:偶数版如2.6.X 发展中的版本:奇数版如2.5.X linux distribution包含:linux kernel + free software + documentati ...
- nginx的日常应用
nginx的配置文件nginx.conf配置详解如下: user nginx nginx ; Nginx用户及组:用户 组.window下不指定 worker_processes ; 工作进程:数目. ...
- 简单C#、asp.net mvc验证码的实现
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Text;u ...
- css选择器万年不变的优先级和权重
我们在使用CSS对网页元素定义样式时经常会遇到这种情况:要对一般元素应用一般样式,然后在更特殊的元素上覆盖它们.那么我们怎么样来保证我们所新定义的元素样式能覆盖目标元素上原有的样式呢? 在CSS中,会 ...
- 用SQL语句将数据表中的数据保存为JSON格式
没有找到好的工具,只想到了拼字符串的方式,用 NVARCHAR(MAX) 可能有截断,不推荐使用,方法中使用了 FOR XML PATH('') 实现,有关其使用方法参考这里 表结构: SQL ...
- 关于Hibernate的Dialect
org.hibernate HibernateException Dialect must be explicitly set :*** 使用Hibernate,有时候会遇到类似上面的异常. 使用 ...