样本数据:利用mongodb自带客户端直接复制进去就可以了
db.users.drop();
var user1 = {
"username" : "lison",
"country" : "china",
"address" : {
"aCode" : "411000",
"add" : "长沙"
},
"favorites" : {
"movies" : ["杀破狼2","战狼","雷神1"],
"cites" : ["长沙","深圳","上海"]
},
"age" : 18,
"salary":NumberDecimal("18889.09"),
"lenght" :1.79,
"comments" : [
{
"author" : "lison1",
"content" : "lison评论1",
"commentTime" : ISODate("2017-01-06T04:26:18.354Z")
"666",30
},
{
"author" : "lison2",
"content" : "lison评论2",
"commentTime" : ISODate("2017-02-06T04:26:18.354Z")
},
{
"author" : "lison3",
"content" : "lison评论3",
"commentTime" : ISODate("2017-03-06T04:26:18.354Z")
},
{
"author" : "lison4",
"content" : "lison评论4",
"commentTime" : ISODate("2017-04-06T04:26:18.354Z")
},
{
"author" : "lison5",
"content" : "lison是苍老师的小迷弟",
"commentTime" : ISODate("2017-05-06T04:26:18.354Z")
},
{
"author" : "lison6",
"content" : "lison评论6",
"commentTime" : ISODate("2017-06-06T04:26:18.354Z")
},
{
"author" : "lison7",
"content" : "lison评论7",
"commentTime" : ISODate("2017-07-06T04:26:18.354Z")
},
{
"author" : "lison8",
"content" : "lison评论8",
"commentTime" : ISODate("2017-08-06T04:26:18.354Z")
},
{
"author" : "lison9",
"content" : "lison评论9",
"commentTime" : ISODate("2017-09-06T04:26:18.354Z")
}
]
};
var user2 = {
"username" : "sean",
"country" : "English",
"address" : {
"aCode" : "311000",
"add" : "地址"
},
"favorites" : {
"movies" : ["复仇者联盟","战狼","雷神1"],
"cites" : ["西安","东京","上海"]
},
"age" : 24,
"salary":NumberDecimal("7889.09"),
"lenght" :1.35,
"comments" : [
{
"author" : "lison1",
"content" : "lison评论1",
"commentTime" : ISODate("2017-10-06T04:26:18.354Z")
},
{
"author" : "lison6",
"content" : "lison评论6",
"commentTime" : ISODate("2017-11-06T04:26:18.354Z")
},
{
"author" : "lison12",
"content" : "lison评论12",
"commentTime" : ISODate("2017-11-06T04:26:18.354Z")
}
]
};
var user3 ={
"username" : "jack",
"country" : "japan",
"address" : {
"aCode" : "411000",
"add" : "长沙"
},
"favorites" : {
"movies" : ["肉蒲团","一路向西","倩女幽魂"],
"cites" : ["东莞","深圳","东京"]
},
"age" : 22,
"salary":NumberDecimal("6666.66"),
"lenght" :1.85,
"comments" : [
{
"author" : "lison1",
"content" : "lison评论1",
"commentTime" : ISODate("2017-10-06T04:26:18.354Z")
},
{
"author" : "lison22",
"content" : "lison评论6",
"commentTime" : ISODate("2017-11-06T04:26:18.354Z")
},
{
"author" : "lison16",
"content" : "lison评论12",
"commentTime" : ISODate("2017-11-06T04:26:18.354Z")
}
]
};
var user4 =
{
"username" : "mark",
"country" : "USA",
"address" : {
"aCode" : "411000",
"add" : "长沙"
},
"favorites" : {
"movies" : ["蜘蛛侠","钢铁侠","蝙蝠侠"],
"cites" : ["青岛","东莞","上海"]
},
"age" : 20,
"salary":NumberDecimal("6398.22"),
"lenght" :1.77
};
var user5 =
{
"username" : "test",
"country" : "UK",
"address" : {
"aCode" : "411000",
"add" : "TEST"
},
"favorites" : {
"movies" : ["蜘蛛侠","钢铁侠","蝙蝠侠"],
"cites" : ["青岛","东莞","上海"]
},
"salary":NumberDecimal("1969.88")
};
db.users.insert(user1);
db.users.insert(user2);
db.users.insert(user3);
db.users.insert(user4);
db.users.insert(user5);
1.查询username是lison的评论中的前三条,只显示评论,不显示个人信息
db.users.find({"username":"lison"},{"comments":{"$slice":[3,3]},"$elemMatch":""}).pretty()
db.users.find({"username":"lison"},{"comments":{"$slice":[3,3]},"_id":1}).pretty()
{
"_id" : ObjectId("5a6ace9f91764e28cf11fe51"),
"comments" : [
{
"author" : "lison3",
"content" : "lison评论3",
"commentTime" : ISODate("2017-12-06T04:26:18.354Z")
},
{
"author" : "lison4",
"content" : "lison评论4",
"commentTime" : ISODate("2017-12-06T04:26:18.354Z")
},
{
"author" : "lison5",
"content" : "lison评论5",
"commentTime" : ISODate("2017-12-06T04:26:18.354Z")
}
]
}
2.数组单元素查询:查询数组中包含“蜘蛛侠”
db.users.find({"favorites.movies":"蜘蛛侠"})
3.数组精确查找(顺序和内容完全一致)
db.users.find({"favorites.movies":[ "肉蒲团", "一路向西", "倩女幽魂"]})
4.数组多元素查询(全部包含,但是和顺序无关)
> db.users.find({"favorites.movies":{"$all":[ "雷神1", "战狼" ]}},{"favorites.movies":1})
> db.user.find({"favorites.movies":{"$in":["复仇者联盟","战狼"]}})包含其中一个电影就可以匹配
5.索引查询
db.users.find({"favorites.movies.0":"杀破狼2"},{"favorites.movies":1})
查询数组中第一个为“杀破狼2”的文档
6.返回数组子集
db.users.find({},{"favorites.movies":{"$slice":[1,2]},"favorites":1})
上面的都是字符串数组的选择查询,下面来看对象数组的选择查询
- 单元素查询:对象数组精确查找
db.users.find({"comments":{
"author" : "lison6",
"content" : "lison评论6"}})
2.查找lison1 或者 lison12评论过的user ($in查找符)
db.users.find({"comments.author":{"$in":["lison1","lison12"]}}).pretty()
备注:跟数量无关,跟顺序无关;
3.查找lison1 和 lison12都评论过的user
db.users.find({"comments.author":{"$all":["lison1","lison2"]}}).pretty()
备注:跟数量有关,跟顺序无关;
4.查找评论对象数组中author是lison5并且coment是lison评论5的人(对象数组多元素匹配)
db.users.find({"comments":{"$elemMatch":{"author" : "lison5",
"content" : "lison评论5"}}}) .pretty()
更新操作
1.删除某一个字段属性,把数据中username:lison的人的address和favorites.movies删掉,注意:值和键都删掉了
> db.users.update({ "username" : "lison"},{"$unset":{"address":"","favorites.movies":""}})
2.批量更新某个键的名字
> db.users.updateMany({"username":"test"},{"$rename":{"country":"guojia","favorites":"爱好"}})
{
"_id" : ObjectId("5a75629a1b4a36051126af17"),
"username" : "test",
"address" : {
"aCode" : "411000",
"add" : "TEST"
},
"salary" : NumberDecimal("1969.88"),
"guojia" : "UK",
"爱好" : {
"movies" : [
"蜘蛛侠",
"钢铁侠",
"蝙蝠侠"
],
"cites" : [
"青岛",
"东莞",
"上海"
]
}
}
3.批量更新多个键的值
> db.users.updateMany({"username":"test"},{"$set":{"username":"test1","address.add":"test"}})
{
"_id" : ObjectId("5a75629a1b4a36051126af17"),
"username" : "test1",
"address" : {
"aCode" : "411000",
"add" : "test"
},
"salary" : NumberDecimal("1969.88"),
"guojia" : "UK",
"爱好" : {
"movies" : [
"蜘蛛侠",
"钢铁侠",
"蝙蝠侠"
],
"cites" : [
"青岛",
"东莞",
"上海"
]
}
}
4.更新普通数组的值($addToSet和$each的使用)
> db.users.updateMany({"username":"test1"},{"$addToSet":{"爱好.movies":{"$each":["电影1","电影2"]}}})
{
"_id" : ObjectId("5a75629a1b4a36051126af17"),
"username" : "test1",
"address" : {
"aCode" : "411000",
"add" : "test"
},
"salary" : NumberDecimal("1969.88"),
"guojia" : "UK",
"爱好" : {
"movies" : [
"蜘蛛侠",
"钢铁侠",
"蝙蝠侠",
"电影1",
"电影2"
],
"cites" : [
"青岛",
"东莞",
"上海"
]
}
}
5.添加两条评论信息(注意更新用$set,非全量更新)
db.users.update({"username":"test1"},{"$set":{"comm":[{"cname":"cang","context":"苍老师说话了"},{"cname":"boduo","context":"波多老师评论了"}]}})
{
"_id" : ObjectId("5a75629a1b4a36051126af17"),
"username" : "test1",
"address" : {
"aCode" : "411000",
"add" : "test"
},
"salary" : NumberDecimal("1969.88"),
"guojia" : "UK",
"爱好" : {
"movies" : [
"蜘蛛侠",
"钢铁侠",
"蝙蝠侠",
"电影1",
"电影2"
],
"cites" : [
"青岛",
"东莞",
"上海"
]
},
"comm" : [
{
"cname" : "cang",
"context" : "苍老师说话了"
},
{
"cname" : "boduo",
"context" : "波多老师评论了"
}
]
}
6.再添加两条评论信息并按照cname排序(添加的是对象数组,用$push和$each,排序用$sort)
db.users.updateOne({"username":"test1"},{
"$push":{
"comm":{
"$each":[{"cname":"masheng","context":"masheng评论"},
{"cname":"lalal","context":"lalal评论"}
],
"$sort":{"cname":1}
}
}
})
"comm" : [
{
"cname" : "boduo",
"context" : "波多老师评论了"
},
{
"cname" : "cang",
"context" : "苍老师说话了"
},
{
"cname" : "lalal",
"context" : "lalal评论"
},
{
"cname" : "masheng",
"context" : "masheng评论"
}
]
7.新增一条评论(不是多条,所以不用each,只需要$push)
db.users.updateOne({"username":"test1"},
{"$push":{"comm":{"author":"lison23","content":"ydddyyytttt"}}
})
8.删除上面那条评论($pull)
db.users.update({"username":"test1"},
{"$pull":{"comm":{"author":"lison23"}}})