一、介绍

        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 } }
>