1. 准备数据 > db.blog.posts.insert(
... {
... "title":"A blog post",
... "content":"..."
... }
... )
> show collections
blog.posts
foo
system.indexes |
2. 添加一个评论comments,评论中有名字,email,content可以看作一个集合体,用数组表示,用数组修改器"$push”,它会向数组末尾加入一个元素,如果数组不存在,则会创建这个数组 > db.blog.posts.update(
... {
... "title":"A blog post"
... }
... ,
... {
... "$push":
... {
... "comments":
... {
... "name":"bob",
... "email":"bob@example.com",
... "content":"good post."
... }
... }
... }
... )
> db.blog.posts.find().pretty()
{
"_id" : ObjectId("53f74fda78a0a91d1242f0ed"),
"comments" : [
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "...",
"title" : "A blog post"
} |
3. “$ne”和"$addToSet”,如果一个值不在数组里面,则加进去 > db.users.insert(
... {
... "username":"joe",
... "emails":[
... "joe@example.com",
... "joe@gmail.com",
... "joe@yahoo.com"
... ]
... }
... ) “$ne”先判断是否存在,插入的时候要借助”$push” > db.users.update(
... {"_id" : ObjectId("53f75dd678a0a91d1242f0ef"),"emails":{"$ne":"joe@example.com"}}, ... {"$push":{"emails":"joe@example.com"}}
... )
> db.users.find().pretty()
{
"_id" : ObjectId("53f75dd678a0a91d1242f0ef"),
"username" : "joe",
"emails" : [
"joe@example.com",
"joe@gmail.com",
"joe@yahoo.com"
]
} “$addToSet”,先判断,插入的时候不需要借助”$push” > db.users.update(
... {"_id" : ObjectId("53f75dd678a0a91d1242f0ef")},
... {"$addToSet":{"emails":"joe@163.com"}}
... )
> db.users.find().pretty()
{
"_id" : ObjectId("53f75dd678a0a91d1242f0ef"),
"emails" : [
"joe@example.com",
"joe@gmail.com",
"joe@yahoo.com",
"joe@163.com"
],
"username" : "joe"
} |
4. “$addToSet”和”$each”组合起来,可以添加多个值 > db.users.update(
... {"_id" : ObjectId("53f7645b78a0a91d1242f0f0")},
... {"$addToSet":
... {"emails":{"$each":["joe@php.net","joe@qq.com","joe@hostmail.com"]}}
... }
... )
> db.users.find().pretty()
{
"_id" : ObjectId("53f7645b78a0a91d1242f0f0"),
"emails" : [
"joe@example.com",
"joe@gmail.com",
"joe@yahoo.com",
"joe@163.com",
"joe@hostmail.com",
"joe@php.net",
"joe@qq.com"
],
"username" : "joe"
} |
5. “$pop”删除数组的任何一端 删除尾端:
> db.users.update(
... {"_id" : ObjectId("53f7645b78a0a91d1242f0f0")},
... {"$pop":{"emails":1}}
... )
> db.users.find().pretty()
{
"_id" : ObjectId("53f7645b78a0a91d1242f0f0"),
"emails" : [
"joe@example.com",
"joe@gmail.com",
"joe@yahoo.com",
"joe@163.com",
"joe@hostmail.com",
"joe@php.net"
],
"username" : "joe"
} 删除头: > db.users.update(
... {"_id" : ObjectId("53f7645b78a0a91d1242f0f0")},
... {"$pop":{"emails":-1}}
... )
> db.users.find().pretty()
{
"_id" : ObjectId("53f7645b78a0a91d1242f0f0"),
"emails" : [
"joe@gmail.com",
"joe@yahoo.com",
"joe@163.com",
"joe@hostmail.com",
"joe@php.net"
],
"username" : "joe"
} |
6. “$pull”根据特定的条件删除 > db.users.update(
... {"_id" : ObjectId("53f7645b78a0a91d1242f0f0")},
... {"$pull":{"emails":"joe@php.net"}}
... )
> db.users.find().pretty()
{
"_id" : ObjectId("53f7645b78a0a91d1242f0f0"),
"emails" : [
"joe@gmail.com",
"joe@yahoo.com",
"joe@163.com",
"joe@hostmail.com"
],
"username" : "joe"
} |
7. 数组定位修改器”$” > db.blog.posts.find().pretty()
{
"_id" : ObjectId("53f7771678a0a91d1242f0f1"),
"comments" : [
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "...",
"title" : "A blog post"
}
>
> db.blog.posts.update(
... {"comments.content" : "good post."},
... {"$set":{"comments.$.content":"bad post."}}
... )
> db.blog.posts.find().pretty()
{
"_id" : ObjectId("53f7771678a0a91d1242f0f1"),
"comments" : [
{
"content" : "bad post.",
"email" : "bob@example.com",
"name" : "bob"
}
],
"content" : "...",
"title" : "A blog post"
}
注明:使用”$”的时候第一个参数和"comments.$.content"指向同一种属性 否则会抱错:can't append to array using string field name [$]
用数组下标,不需要 第一个参数和第二个参数的键要匹配 > db.blog.posts.update(
... {"_id" : ObjectId("53f7771678a0a91d1242f0f1")},
... {"$set":{"comments.0.content":"good post."}}
... )
> db.blog.posts.find().pretty()
{
"_id" : ObjectId("53f7771678a0a91d1242f0f1"),
"comments" : [
{
"content" : "good post.",
"email" : "bob@example.com",
"name" : "bob"
}
],
"content" : "...",
"title" : "A blog post"
} |
|