【知识小课堂】 mongodb 之字段中的【 数组】、【内嵌文档】
2014-07-10 12:12
435 查看
一、介绍
MONGODB 的表结构 很灵活 。主要还是因为 字段中可以包含 【 数组】、【内嵌文档】。现在简单介绍一下 字段中的【 数组】、【内嵌文档】相关的一些操作
(为了方便理解,还是以表来理解mongodb 里的文档)
数组1
1、数组
是一组数,可以是有序对象,也可以是无序对象可以是不同数据类型,也可以相同数据类型
{“things”:[“pie”,3.14]} {name:”Joe”, Age:25, Status:”A”, Groups:[“news”,”sports”]}
2、文档
文档 可以做为【键】的值,这样的文档就是内嵌文档,这样处理的目的是使用数据组织更为自然。如果键多时,在每次访问时只取需要的键,而非全部键都取。从而优化读取效率。
{ name:{first:”John”,last:” Doe”}, Age:23, Address:{ street:”nanjing 100#”,city:”Nanchang”,province:”Jiangxi”}, Tel:{ tel:”0792-12133232”,mobile:”134232232323”,fax:”0792-243453433”} }
3、数组查询
> db.food.find() { "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] } { "_id" : 2, "fruit" : [ "apple", "kumquat", "orange" ] } { "_id" : 3, "fruit" : [ "cherry", "banana", "apple" ] }
3.1 查询包含”apple”或者”banana”的文档,则使用”$in”
> db.food.find({fruit:{$in:[" apple “,”banana”]}}) { "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] } { "_id" : 2, "fruit" : [ "apple", "kumquat", "orange" ] } { "_id" : 3, "fruit" : [ "cherry", "banana", "apple" ] }
3.2 查询既包含”apple”并且又包含”banana”的文档
> db.food.find({fruit:{$all:["apple","banana"]}}) { "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] } { "_id" : 3, "fruit" : [ "cherry", "banana", "apple" ] }
3.3 使用”$size“可以查询指定长度的数组
我先插入一条新的记录:> db.food.insert({fruit:["Watermelon"]})
查询只有一个水果列表的新添加记录:
> db.food.find({fruit:{$size:1}},{_id:0,fruit:1}) { "fruit" : [ "Watermelon" ] }
查询有3种水果的记录:
> db.food.find({fruit:{$size:3}},{_id:0,fruit:1}) { "fruit" : [ "apple", "banana", "peach" ] } { "fruit" : [ "apple", "kumquat", "orange" ] } { "fruit" : [ "cherry", "banana", "apple" ] } >
3.4 使用 ”$slice“ 指定 返回数据中的某几条数据
原数据如下:> db.blog.findOne() { "_id" : 1, "title" : "Hello World", "content" : "My first blog.", "comments" : [ { "name" : "joe", "content" : "test" }, {"name" : "Zhang", "content" : "so so .." }, { "name" : "Li", "content" : "very good" } ] } >
一条博客里有多条点评。但我要指定返回两条点评。(
$slice:[1,2] 从[1]开始,取2条 ,数组序号以0 开始。
)
> db.blog.findOne({},{"comments":{$slice:[1,2]}}) { "_id" : 1, "title" : "Hello World", "content" : "My first blog.", "comments" : [ { "name" : "Zhang", "content" : "so so .." }, { "name" : "Li", "content" : "very good" } ] } >
当然,你也还可以指定显示特别指定要返回的点评记录:
如:最后一条点评
> db.blog.findOne({},{"comments":{$slice:-1}}) { "_id" : 1, "title" : "Hello World", "content" : "My first blog.", "comments" : [ { "name" : "Li", "content" : "very good" } ] }
前两条:
> db.blog.findOne({},{"comments":{$slice:2}}) { "_id" : 1, "title" : "Hello World", "content" : "My first blog.", "comments" : [ { "name" : "joe", "content" : "test" }, { "name" : "Zhang", "content" : "so so .." } ] } >
3.5 数组更新、添加
> db.food.insert({_id:4,fruit:['neo4j','mysql','redis']}) > db.food.find({_id:4}) { "_id" : 4, "fruit" : [ "neo4j", "mysql", "redis" ] } > db.food.update({_id:4},{“$pull”:{“fruit”:“neo4j”}}) #删除指定值 > db.food.find({_id:4}) { "_id" : 4, "fruit" : [ "mysql", "redis" ] } > db.food.update({_id:4},{“$pop”:{“fruit”:1}}) #删除最后一个值 > db.food.find({_id:4}) { "_id" : 4, "fruit" : [ "mysql" ] } > db.food.update({_id:4},{“$push”:{“fruit”:“mongo”}}) #添加一个值 > db.food.find({_id:4}) { "_id" : 4, "fruit" : [ "mysql", "mongo" ] } >
3.6 内嵌子文档查询
这个用得比较多,大家也很熟悉,就象对象引用是一样的。多层间使用“." 但如果层次越来越多,在查询,更新时。就会越来越不方便了。容易出错。内嵌子文档查询
> db.tst_3layer.find() { "_id" : 1, "po" : { "prod" : { "_id" : 1, "name" : "mongobook" }, "qty" : 1, "price" : 10, "amount" : 10 } } { "_id" : 2, "po" : { "prod" : { "_id" : 2, "name" : "oracle book" }, "qty" : 2, "price" : 230, "amount" : 460 } } > db.tst_3layer.find({"po.prod._id":1}) { "_id" : 1, "po" : { "prod" : { "_id" : 1, "name" : "mongobook" }, "qty" : 1, "price" : 10, "amount" : 10 } } >
相关文章推荐
- mongodb_数据类型(null/字符串/数字/日期/内嵌文档/数组等)(转并学习)
- mongodb 对内嵌文档(数组) group分页查询,并设置查询条件
- mongodb数组与内嵌文档查询
- mongodb_数据类型(null/字符串/数字/日期/内嵌文档/数组等)
- MongoDB .Net Driver(C#驱动) - 内嵌数组/嵌入文档的操作(增加、删除、修改、查询(Linq 分页))
- MongoDB查询(数组、内嵌文档和$where)
- mongodb_数据类型(null/字符串/数字/日期/内嵌文档/数组等)
- MongoDB——$elemMatch(数组的内嵌文档查询匹配)
- mongodb 数据类型(null/字符串/数字/日期/内嵌文档/数组等)
- MongoDB查询 之 数组、内嵌文档和$where
- mongodb中删除数组内嵌对象文档
- MongoDB查询(数组、内嵌文档和$where)
- Spring data mongodb 聚合,投射,内嵌数组文档分页.
- 学习MongoDB 五: MongoDB查询(数组、内嵌文档)(二)
- C# mongodb中内嵌文档数组条件查询
- MongoDB查询(数组、内嵌文档和$where)
- 学习MongoDB--(4-2):MongoDB查询(数组、内嵌文档和$where)
- 学习MongoDB--(4-2):MongoDB查询(数组、内嵌文档和$where)
- MongoDB .Net Driver(C#驱动) - 内嵌数组/嵌入文档的操作(增加、删除、修改、查询(Linq 分页))
- morphia查询mongodb内嵌文档-数组查询$elemMatch|hasThisElement实例