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

MongoDB CRUD Operations

Iterate a Cursor in the mongo Shell

作者悦光阴
本文版权归作者和博客园所有,欢迎转载,但未经作者同意,必须保留此段声明,且在文章页面醒目位置显示原文连接,否则保留追究法律责任的权利。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值