MONGODB 的表结构 很灵活 。主要还是因为 字段中可以包含 【 数组】、【内嵌文档】。
现在简单介绍一下 字段中的【 数组】、【内嵌文档】相关的一些操作
(为了方便理解,还是以表来理解mongodb 里的文档)
数组1
1、数组是一组数,可以是有序对象,也可以是无序对象
可以是不同数据类型,也可以相同数据类型
{“things”:[“pie”,3.14]} {name:”Joe”, Age:25, Status:”A”, Groups:[“news”,”sports”]}
文档 可以做为【键】的值,这样的文档就是内嵌文档,这样处理的目的是使用数据组织更为自然。
如果键多时,在每次访问时只取需要的键,而非全部键都取。从而优化读取效率。
{ name:{first:”John”,last:” Doe”}, Age:23, Address:{ street:”nanjing 100#”,city:”Nanchang”,province:”Jiangxi”}, Tel:{ tel:”0792-12133232”,mobile:”134232232323”,fax:”0792-243453433”} }
> db.food.find() { "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] } { "_id" : 2, "fruit" : [ "apple", "kumquat", "orange" ] } { "_id" : 3, "fruit" : [ "cherry", "banana", "apple" ] }
> 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" ] }
> db.food.find({fruit:{$all:["apple","banana"]}}) { "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] } { "_id" : 3, "fruit" : [ "cherry", "banana", "apple" ] }
我先插入一条新的记录:
> 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" ] } >
原数据如下:
> 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 .." } ] } >
> 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" ] } >
这个用得比较多,大家也很熟悉,就象对象引用是一样的。多层间使用“." 但如果层次越来越多,在查询,更新时。就会越来越不方便了。容易出错。
内嵌子文档查询> 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 } } >