在MongoDB的情势中,我们常常将一些数据存储到数组范例中,即我们罕见的嵌套情势设想的一种完成体式格局。数组的这类设想完成体式格局在关联数据库中是没有或许说不罕见的。所以,经由过程本文我们来梳理一下MongoDB的数组的相干操纵。关于数组的操纵能够分红两类,一类是数组操纵符,另一个是数组运算修饰符。
数组操纵符
操纵符 | 完成功用 |
$ | 依据查询选择器定位要更新的文档 |
$push | 增添值到数组中 |
$pushAll | 增添数组到一个数组中。(将被$rach庖代) |
$addToSet | 增添值到数组中,反复了也不处置惩罚 |
$pop | 从数组中删除第一个或许末了一个值。 |
$pull | 从数组中删除婚配查询前提的值。 |
$pullAll | 从数组中删除多个值。 |
数组运算修饰符
修饰符 | 完成功用 |
$each | 与$push和$addToSet一同运用来操纵多个值。 |
$slice | 与$push和$each一同运用来减少更新后数组的大小。 |
$sort | 与$push、$each、$slice一同来排序数组中的子文档。 |
1.$push操纵符
1.1 语法及功用形貌
$push 重要用来向数组中增添元素。
语法:
{ $push: { <field1>: <value1>, ... } }
默许情况下,它会在数组尾部增添一个零丁的元素。
1.2 操纵案例
假如我们有一个门生效果的鸠合studentscore,其文档花样以下:
{ "_id" : 1, "name" : "xiaoming", "score" : [ { "math" : 99, "english" : 89 } ] } { "_id" : 2, "name" : "xiaohong", "score" : [ { "math" : 98, "english" : 96 } ] }
个中的需求为,更新_id 为1的文档纪录,在分数数组的字段上,增添 物理学的效果,修正代码为
db.studentscore.update({_id:1},{$push: {score:{"physics":100}}})
修正后,效果查询以下:
{ "_id" : 1, "name" : "xiaoming", "score" : [ { "math" : 99, "english" : 89 }, { "physics" : 100 } ] } { "_id" : 2, "name" : "xiaohong", "score" : [ { "math" : 98, "english" : 96 } ] }
1.3 连系$each修饰符,批量插进去
假如一次将多个值增添到数组中,可连系 数组修正符 $each 一同运用。
比方,我们将小红的(_id =2)的物理效果、化学效果、生物效果一同增添到文档中。实行的语句以下:
db.studentscore.update({ _id: 2 }, { $push: { score: { $each: [{ "physics": 100 }, { "chemistry": 90 }, { "biology": 99 }] } } } )
查询的效果以下:
{ "_id" : 1, "name" : "xiaoming", "score" : [ { "math" : 99, "english" : 89 }, { "physics" : 100 } ] } { "_id" : 2, "name" : "xiaohong", "score" : [ { "math" : 98, "english" : 96 }, { "physics" : 100 }, { "chemistry" : 90 }, { "biology" : 99 } ] }
1.4 数组修饰符 $sort 和 $slice的运用
前面讲了$each 数组运算修饰符,那我们再举一个例子,将盈余的两个修饰符一同讲解了好了($sort 和 $slice)
比方,我们有文档纪录以下:
{ "_id" : 5, "quizzes" : [ { "wk": 1, "score" : 10 }, { "wk": 2, "score" : 8 }, { "wk": 3, "score" : 5 }, { "wk": 4, "score" : 6 } ] }
如今我们,有个需求,就是 首先向文档的quizzes数组字段,追加三个纪录,然后,我们再根据score排序,拔取数组中的前三个元素。
db.students.update( { _id: 5 }, { $push: { quizzes: { $each: [ { wk: 5, score: 8 }, { wk: 6, score: 7 }, { wk: 7, score: 6 } ], $sort: { score: -1 }, $slice: 3 } } } )
更新后的效果显现以下:
{ "_id" : 5, "quizzes" : [ { "wk" : 1, "score" : 10 }, { "wk" : 2, "score" : 8 }, { "wk" : 5, "score" : 8 } ]}
$slice操纵修饰符是在MongoDB 2.4 里增添的,其目标是轻易治理常常更新的数组。当向数组增添值然则不想数组太大的时刻,这个操纵符异常有效。它必需与$push、$each操纵符一同运用,许可用来剪短数组的大小、删除旧的值。
与$slice操纵修饰符很像,MongoDB 2.4 新增了$sort操纵修饰符,协助更新数组。当运用$push和$slice时,有时刻要先排序再删除它们。
2. $pop 操纵符
2.1 语法及功用形貌
$pop操纵符能够完成从数组中删除第一个或许是最好一个元素。
{ $pop: { <field>: <-1 | 1>, ... } }
参数为-1 ,代表要删除数组中的第一个元素;参数为1 ,代表要删除数组中的末了一个元素。
2.2 操纵案例
比方鸠合students 中有以下文档:
{ _id: 1, scores: [ 8, 9, 10 ] }
我们的需求是要把数组中的第一个元素(效果为8)移除,SQL 语句以下:
db.students.update( { _id: 1 }, { $pop: { scores: -1 } } )
更新后,文档以下
{ _id: 1, scores: [ 9, 10 ] }
继承演示,假如在现有的基础上,我们须要进一步把数组的末了一个元素移除(效果为10),更新的sQL以下:
db.students.update( { _id: 1 }, { $pop: { scores: 1 } } )
查询效果 以下:
{ _id: 1, scores: [ 9 ] }
3. $pull操纵符
3.1 语法及功用形貌
$pull是$pop的庞杂情势。运用$pull,能够经由过程值准确指定要删除的元素。
语法花样
{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }
3.2 操纵案例
3.2.1 移除数组中即是指定值的元素
测试文档以下:
{ _id: 1, fruits: [ "apples", "pears", "oranges", "grapes", "bananas" ], vegetables: [ "carrots", "celery", "squash", "carrots" ]} { _id: 2, fruits: [ "plums", "kiwis", "oranges", "bananas", "apples" ], vegetables: [ "broccoli", "zucchini", "carrots", "onions" ]}
操纵要求是将 数组字段fruits中的"apples"
and "oranges" 移除,还要将vegetables数组字段中的"carrots" 移除,其更新语句以下:
db.stores.update( { }, { $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }, { multi: true } )
更新后的效果以下:
{ "_id" : 1, "fruits" : [ "pears", "grapes", "bananas" ], "vegetables" : [ "celery", "squash" ]} { "_id" : 2, "fruits" : [ "plums", "kiwis", "bananas" ], "vegetables" : [ "broccoli", "zucchini", "onions" ]}
此时,鸠合文档中,fruit的数组字段 没有apples也没有
oranges,vegetables数组字段也没有了carrots。
3.2.2 移除数组中满足指定前提的元素
假如我们有一个 profiles 的鸠合,其文档花样以下:
{ _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] }
我们要把votes大于即是6的元素移除,其语句以下:
db.profiles.update( { _id: 1 }, { $pull: { votes: { $gte: 6 } } } )
更新后的效果以下:
{ _id: 1, votes: [ 3, 5 ] }
3.2.3 移除数组中内嵌子文档(即此时数组元素是子文档,每个{}中的内容是一个数组元素)
假定我们有一个关于 观察的鸠合 survey,其数据以下:
{ _id: 1, results: [ { item: "A", score: 5 }, { item: "B", score: 8, comment: "Strongly agree" } ]} { _id: 2, results: [ { item: "C", score: 8, comment: "Strongly agree" }, { item: "B", score: 4 } ]}
需求是将 score 为
8
而且 item
为 "B"的元素移除
db.survey.update( { }, { $pull: { results: { score: 8 , item: "B" } } }, { multi: true } )
更新后的文档以下:
{ "_id" : 1, "results" : [ { "item" : "A", "score" : 5 } ]} { "_id" : 2, "results" : [ { "item" : "C", "score" : 8, "comment" : "Strongly agree" }, { "item" : "B", "score" : 4 } ]}
3.2.4 假如数组范例的元素还内嵌一个数组(数组包数组),就要迥殊警惕了。
此时就要用到 $elemMatch操纵符。
比方 文档花样以下:
{ _id: 1, results: [ { item: "A", score: 5, answers: [ { q: 1, a: 4 }, { q: 2, a: 6 } ] }, { item: "B", score: 8, answers: [ { q: 1, a: 8 }, { q: 2, a: 9 } ] } ] } { _id: 2, results: [ { item: "C", score: 8, answers: [ { q: 1, a: 8 }, { q: 2, a: 7 } ] }, { item: "B", score: 4, answers: [ { q: 1, a: 0 }, { q: 2, a: 8 } ] } ] }
须要将 results数组字段 移除,移除的前提是 results数组字段中的answers字段,相符 q
为 2
and a
大于即是 8。
db.survey.update( { }, { $pull: { results: { answers: { $elemMatch: { q: 2, a: { $gte: 8 } } } } } }, { multi: true } )
更新后的数据以下:
{ "_id" : 1, "results" : [ { "item" : "A", "score" : 5, "answers" : [ { "q" : 1, "a" : 4 }, { "q" : 2, "a" : 6 } ] } ] } { "_id" : 2, "results" : [ { "item" : "C", "score" : 8, "answers" : [ { "q" : 1, "a" : 8 }, { "q" : 2, "a" : 7 } ] } ] }
4.$addToSet
4.1 语法及功用形貌
运用$addToSet也会往数组背面增添值,然则它比较特别:它只会增添数组里不存在的值。
{ $addToSet: { <field1>: <value1>, ... } }
4.2 操纵案例
假如有一个鸠合 inventory
花样以下
{ _id: 1, item: "polarizing_filter", tags: [ "electronics", "camera" ] }
我们愿望向向字段 tags 数组 ,增添一个元素accessories,则更新语句以下:
db.inventory.update( { _id: 1 }, { $addToSet: { tags: "accessories" } } )
更新后的效果为
{ "_id" : 1, "item" : "polarizing_filter", "tags" : [ "electronics", "camera", "accessories" ] }
假如想批量的增添假如元素,我们能够连系 $each 操纵符一同运用。
比方以下文档
{ _id: 2, item: "cable", tags: [ "electronics", "supplies" ] }
我们想在字段 tags 数组,增添元素 "camera", "electronics", "accessories",则更新语句以下:
db.inventory.update( { _id: 2 }, { $addToSet: { tags: { $each: [ "camera", "electronics", "accessories" ] } } } )
更新后的效果以下:
{ _id: 2, item: "cable", tags: [ "electronics", "supplies", "camera", "accessories" ]}
4.3 注重点
须要注重是,假如增添的元素是数组花样,则会将新增添的元素保存为数组(将会涌现数组嵌套数组)
比方
{ _id: 1, letters: ["a", "b"] }
实行的语句以下:
db.test.update( { _id: 1 }, { $addToSet: {letters: [ "c", "d" ] } } )
查询构造显现为
{ _id: 1, letters: [ "a", "b", [ "c", "d" ] ] }
以上就是MongoDB中数组范例的操纵(代码示例)的细致内容,更多请关注ki4网别的相干文章!