首先在MongoDB里面是支持数组保存的,一旦支持了数组保存,就需要针对于数组的数据进行匹配。
范例:保存一部分数组内容
db.emp.insert({"name":"刘A","sex":"男","age":35,"sal":8000,"loc":"北京","course":["语文","数学","英语","音乐","政治"]});
db.emp.insert({"name":"刘B","sex":"男","age":35,"sal":8000,"loc":"北京","course":["语文","数学"]});
db.emp.insert({"name":"刘C","sex":"男","age":35,"sal":8000,"loc":"北京","course":["语文","数学","英语"]});
db.emp.insert({"name":"刘D","sex":"男","age":35,"sal":8000,"loc":"北京","course":["语文","数学","政治"]});
db.emp.insert({"name":"刘E","sex":"男","age":35,"sal":8000,"loc":"北京","course":["语文","政治"]});
> db.emp.find().pretty();
{
"_id" : ObjectId("599108423268c8e84253be26"),
"name" : "赵一",
"sex" : "男",
"age" : 30,
"sal" : 1000,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be27"),
"name" : "钱二",
"sex" : "女",
"age" : 22,
"sal" : 5000,
"loc" : "上海"
}
{
"_id" : ObjectId("599108423268c8e84253be28"),
"name" : "孙三",
"sex" : "男",
"age" : 40,
"sal" : 2000,
"loc" : "深圳"
}
{
"_id" : ObjectId("599108423268c8e84253be29"),
"name" : "李四",
"sex" : "女",
"age" : 30,
"sal" : 7000,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be2a"),
"name" : "周五",
"sex" : "女",
"age" : 30,
"sal" : 6400,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be2b"),
"name" : "吴六",
"sex" : "男",
"age" : 30,
"sal" : 2500,
"loc" : "重庆"
}
{
"_id" : ObjectId("599108423268c8e84253be2c"),
"name" : "郑七",
"sex" : "女",
"age" : 50,
"sal" : 4700,
"loc" : "成都"
}
{
"_id" : ObjectId("599108433268c8e84253be2d"),
"name" : "王八",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京"
}
{
"_id" : ObjectId("599129a00184ff511bf02b87"),
"name" : "刘A",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"语文",
"数学",
"英语",
"音乐",
"政治"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b88"),
"name" : "刘B",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"语文",
"数学"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b89"),
"name" : "刘C",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"语文",
"数学",
"英语"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b8a"),
"name" : "刘D",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"语文",
"数学",
"政治"
]
}
{
"_id" : ObjectId("599129a20184ff511bf02b8b"),
"name" : "刘E",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"语文",
"政治"
]
}
此时的数据包含有数组内容,而后需要针对数组数据进行判断,可以使用几个运算符:$all、$size、$slice、$elemMatch
范例:查询同时参加语文和数学课程的人员
现在两个数组内容都需要保存,所以使用“{"$all",[内容1,内容2,...]}”
> db.emp.find({"course":{"$all":["语文","数学"]}}).pretty();
{
"_id" : ObjectId("599129a00184ff511bf02b87"),
"name" : "刘A",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"语文",
"数学",
"英语",
"音乐",
"政治"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b88"),
"name" : "刘B",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"语文",
"数学"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b89"),
"name" : "刘C",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"语文",
"数学",
"英语"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b8a"),
"name" : "刘D",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"语文",
"数学",
"政治"
]
}
现在所有显示的人员信息里面包含语文和数学的内容。而如果差一个内容的不会显示。
虽然“$all”计算可以用于数组上,但是也可以用于一个数据的匹配上。
范例:查询位置是“成都”的人员
> db.emp.find({"loc":{"$all":["成都"]}}).pretty();
{
"_id" : ObjectId("599108423268c8e84253be2c"),
"name" : "郑七",
"sex" : "女",
"age" : 50,
"sal" : 4700,
"loc" : "成都"
既然在集合里面现在保存的是数组信息,那么数组就可以利用索引操作,使用“key.index”的方式来定义索引。
范例:查询课程中第二个内容(index=1,索引下标从0开始)为数学的信息
> db.emp.find({"course.1":"数学"}).pretty();
{
"_id" : ObjectId("599129a00184ff511bf02b87"),
"name" : "刘A",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"语文",
"数学",
"英语",
"音乐",
"政治"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b88"),
"name" : "刘B",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"语文",
"数学"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b89"),
"name" : "刘C",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"语文",
"数学",
"英语"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b8a"),
"name" : "刘D",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"语文",
"数学",
"政治"
]
}
范例:要求查询出只参加两门课程的人员
使用“$size”来进行数量控制
> db.emp.find({"course":{"$size":2}}).pretty();
{
"_id" : ObjectId("599129a00184ff511bf02b88"),
"name" : "刘B",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"语文",
"数学"
]
}
{
"_id" : ObjectId("599129a20184ff511bf02b8b"),
"name" : "刘E",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"语文",
"政治"
]
}
发现在进行数据查询的时候,只要内容符合条件,数组的内容就全备显示出来,希望控制返回的数量,可以使用“$slice”进行控制。
范例:返回年龄为35岁的所有人员,但是要求只显示两门参加的课程
> db.emp.find({"age":35},{"course":{"$slice":2}}).pretty();
{
"_id" : ObjectId("599108433268c8e84253be2d"),
"name" : "王八",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京"
}
{
"_id" : ObjectId("599129a00184ff511bf02b87"),
"name" : "刘A",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"语文",
"数学"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b88"),
"name" : "刘B",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"语文",
"数学"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b89"),
"name" : "刘C",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"语文",
"数学"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b8a"),
"name" : "刘D",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"语文",
"数学"
]
}
{
"_id" : ObjectId("599129a20184ff511bf02b8b"),
"name" : "刘E",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"语文",
"政治"
]
}
现在只取得了前两门的信息,那么也可以设置负数取出后两门信息。
> db.emp.find({"age":35},{"course":{"$slice":-2}}).pretty();
{
"_id" : ObjectId("599108433268c8e84253be2d"),
"name" : "王八",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京"
}
{
"_id" : ObjectId("599129a00184ff511bf02b87"),
"name" : "刘A",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"音乐",
"政治"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b88"),
"name" : "刘B",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"语文",
"数学"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b89"),
"name" : "刘C",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"数学",
"英语"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b8a"),
"name" : "刘D",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"数学",
"政治"
]
}
{
"_id" : ObjectId("599129a20184ff511bf02b8b"),
"name" : "刘E",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"语文",
"政治"
]
}
或者只是取出中间部分的信息。
> db.emp.find({"age":35},{"course":{"$slice":[1,2]}}).pretty();
{
"_id" : ObjectId("599108433268c8e84253be2d"),
"name" : "王八",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京"
}
{
"_id" : ObjectId("599129a00184ff511bf02b87"),
"name" : "刘A",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"数学",
"英语"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b88"),
"name" : "刘B",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"数学"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b89"),
"name" : "刘C",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"数学",
"英语"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b8a"),
"name" : "刘D",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"数学",
"政治"
]
}
{
"_id" : ObjectId("599129a20184ff511bf02b8b"),
"name" : "刘E",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"政治"
]
}
在此时设置的两个数据里面,第一个数据表示开始的位置,第二个数据表示个数。
分享标题:MongoDB之数据查询(数组)
本文路径:
http://bzwzjz.com/article/igdjso.html