数组修改器 $push $ne ($addToSet $each ) $pop $pull
$push 向已有数组末尾 增加一个元素,如果数组不存在则创建。
如:
给blog 增加评论数组 ,本来没有 comments 数组 ,则创建了 comments 数组。
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"title" : "My Blog post",
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z")
}
> db.foo.update({"title":"My Blog post"},
... {$push : {"comments":
... {"name":"joe","email":"joe@example.com","content":"nice post."}}})
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"title" : "My Blog post",
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z")
}
> db.foo.update({"title":"My Blog post"},
... {$push : {"comments":
... {"name":"joe","email":"joe@example.com","content":"nice post."}}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"title" : "My Blog post"
}
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"title" : "My Blog post"
}
再增加一条评论 ,插入在 comments 数组 末尾。
> db.foo.update({"title":"My Blog post"},
... {$push : {"comments":
... {"name":"bob","email":"bob@example.com","content":"good post."}}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"title" : "My Blog post"
}
... {$push : {"comments":
... {"name":"bob","email":"bob@example.com","content":"good post."}}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"title" : "My Blog post"
}
$ne 跟 $push 连用
$ne 表示 数组中不存在 这个条件,才执行 后面的条件 ( 跟 $push 连用表示 不存在条件 则 把 它 插入到 数组末尾)。
> db.foo.update({"title":"My Blog post"},
... {$set:{"authors":["joe","bob"]}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"authors" : [
"joe",
"bob"
],
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"title" : "My Blog post"
}
... {$set:{"authors":["joe","bob"]}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"authors" : [
"joe",
"bob"
],
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"title" : "My Blog post"
}
给 authors 键 增加 Richie 用户。
> db.foo.update({"authors":{"$ne":"Richie"}},
... {$push : { "authors":"Richie"}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"authors" : [
"joe",
"bob",
"Richie"
],
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"title" : "My Blog post"
}
... {$push : { "authors":"Richie"}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"authors" : [
"joe",
"bob",
"Richie"
],
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"title" : "My Blog post"
}
增加成功。
增加一个重复的 joe 看看
> db.foo.update({"authors":{"$ne":"joe"}},
... {$push:{"authors":"joe"}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"authors" : [
"joe",
"bob",
"Richie"
],
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"title" : "My Blog post"
}
>
... {$push:{"authors":"joe"}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"authors" : [
"joe",
"bob",
"Richie"
],
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"title" : "My Blog post"
}
>
没有加进去。
$addToSet 可以实现 跟"$ne"相同的功能。
加入已经存在的
> db.foo.update({"title":"My Blog post"},
... {"$addToSet":{"authors":"joe"}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"authors" : [
"joe",
"bob",
"Richie"
],
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"title" : "My Blog post"
}
... {"$addToSet":{"authors":"joe"}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"authors" : [
"joe",
"bob",
"Richie"
],
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"title" : "My Blog post"
}
跟 $ne 一样 没有任何变化
加入 新作者 Apple
> db.foo.update({"title":"My Blog post"},
... {"$addToSet":{"authors":"Apple"}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"authors" : [
"joe",
"bob",
"Richie",
"Apple"
],
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"title" : "My Blog post"
}
... {"$addToSet":{"authors":"Apple"}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"authors" : [
"joe",
"bob",
"Richie",
"Apple"
],
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"title" : "My Blog post"
}
$addToSet 可以跟 $each 组合使用 (x 完成 $ne 和 $push 组合不能完成的 X)
一次添加多个 邮件地址
> db.foo.update({"title":"My Blog post"},
... {"$addToSet":
... {"emails":{"$each":["joe@php.net","joe@example.com","joe@python.org"]}}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"authors" : [
"joe",
"bob",
"Richie",
"Apple"
],
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"emails" : [
"joe@php.net",
"joe@example.com",
"joe@python.org"
],
"title" : "My Blog post"
}
... {"$addToSet":
... {"emails":{"$each":["joe@php.net","joe@example.com","joe@python.org"]}}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"authors" : [
"joe",
"bob",
"Richie",
"Apple"
],
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"emails" : [
"joe@php.net",
"joe@example.com",
"joe@python.org"
],
"title" : "My Blog post"
}
删除数组元素
$pop 删除 数组中指定位置的元素
$pull 删除 数组中指定条件的元素,跟位置无关,会删掉所有匹配条件的元素。
{$pop : { key : 1}} 从数组末尾 删掉一个元素。
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"authors" : [
"joe",
"bob",
"Richie",
"Apple"
],
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"emails" : [
"joe@php.net",
"joe@example.com",
"joe@python.org"
],
"title" : "My Blog post"
}
> db.foo.update({"title":"My Blog post"},
... {"$pop":{"emails":1}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"authors" : [
"joe",
"bob",
"Richie",
"Apple"
],
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"emails" : [
"joe@php.net",
"joe@example.com"
],
"title" : "My Blog post"
}
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"authors" : [
"joe",
"bob",
"Richie",
"Apple"
],
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"emails" : [
"joe@php.net",
"joe@example.com",
"joe@python.org"
],
"title" : "My Blog post"
}
> db.foo.update({"title":"My Blog post"},
... {"$pop":{"emails":1}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"authors" : [
"joe",
"bob",
"Richie",
"Apple"
],
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"emails" : [
"joe@php.net",
"joe@example.com"
],
"title" : "My Blog post"
}
{$pop : { key : -1}}从数组头部删除一个元素。
> db.foo.update({"title":"My Blog post"},
... {"$pop":{"emails":-1}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"authors" : [
"joe",
"bob",
"Richie",
"Apple"
],
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"emails" : [
"joe@example.com"
],
"title" : "My Blog post"
}
... {"$pop":{"emails":-1}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"authors" : [
"joe",
"bob",
"Richie",
"Apple"
],
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"emails" : [
"joe@example.com"
],
"title" : "My Blog post"
}
$pull 删除指定条件的字段,删掉 authors 中的 Apple :
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"authors" : [
"joe",
"bob",
"Richie",
"Apple"
],
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"emails" : [
"joe@example.com"
],
"title" : "My Blog post"
}
> db.foo.update({"title":"My Blog post"},
... {"$pull":{"authors":"Apple"}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"authors" : [
"joe",
"bob",
"Richie"
],
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"emails" : [
"joe@example.com"
],
"title" : "My Blog post"
}
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"authors" : [
"joe",
"bob",
"Richie",
"Apple"
],
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"emails" : [
"joe@example.com"
],
"title" : "My Blog post"
}
> db.foo.update({"title":"My Blog post"},
... {"$pull":{"authors":"Apple"}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdee24389834bfb0bd267a9"),
"authors" : [
"joe",
"bob",
"Richie"
],
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
],
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"emails" : [
"joe@example.com"
],
"title" : "My Blog post"
}
$pull 删除所有匹配条件的元素
> db.foo.update({"title":"My Blog post"},
... {"$set":{"test":[1,1,2,1]}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdefcb789834bfb0bd267ac"),
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"test" : [
1,
1,
2,
1
],
"title" : "My Blog post"
}
> db.foo.update({"title":"My Blog post"},
... {"$pull":{"test":1}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdefcb789834bfb0bd267ac"),
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"test" : [
2
],
"title" : "My Blog post"
}
>
... {"$set":{"test":[1,1,2,1]}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdefcb789834bfb0bd267ac"),
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"test" : [
1,
1,
2,
1
],
"title" : "My Blog post"
}
> db.foo.update({"title":"My Blog post"},
... {"$pull":{"test":1}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdefcb789834bfb0bd267ac"),
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"test" : [
2
],
"title" : "My Blog post"
}
>