首先在MongoDB里面是支持?jǐn)?shù)組保存的,一旦支持了數(shù)組保存,就需要針對(duì)于數(shù)組的數(shù)據(jù)進(jìn)行匹配。
范例:保存一部分?jǐn)?shù)組內(nèi)容
db.emp.insert({"name":"劉A","sex":"男","age":35,"sal":8000,"loc":"北京","course":["語(yǔ)文","數(shù)學(xué)","英語(yǔ)","音樂","政治"]});
db.emp.insert({"name":"劉B","sex":"男","age":35,"sal":8000,"loc":"北京","course":["語(yǔ)文","數(shù)學(xué)"]});
db.emp.insert({"name":"劉C","sex":"男","age":35,"sal":8000,"loc":"北京","course":["語(yǔ)文","數(shù)學(xué)","英語(yǔ)"]});
db.emp.insert({"name":"劉D","sex":"男","age":35,"sal":8000,"loc":"北京","course":["語(yǔ)文","數(shù)學(xué)","政治"]});
db.emp.insert({"name":"劉E","sex":"男","age":35,"sal":8000,"loc":"北京","course":["語(yǔ)文","政治"]});
> 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" : [
"語(yǔ)文",
"數(shù)學(xué)",
"英語(yǔ)",
"音樂",
"政治"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b88"),
"name" : "劉B",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"語(yǔ)文",
"數(shù)學(xué)"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b89"),
"name" : "劉C",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"語(yǔ)文",
"數(shù)學(xué)",
"英語(yǔ)"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b8a"),
"name" : "劉D",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"語(yǔ)文",
"數(shù)學(xué)",
"政治"
]
}
{
"_id" : ObjectId("599129a20184ff511bf02b8b"),
"name" : "劉E",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"語(yǔ)文",
"政治"
]
}
此時(shí)的數(shù)據(jù)包含有數(shù)組內(nèi)容,而后需要針對(duì)數(shù)組數(shù)據(jù)進(jìn)行判斷,可以使用幾個(gè)運(yùn)算符:$all、$size、$slice、$elemMatch
范例:查詢同時(shí)參加語(yǔ)文和數(shù)學(xué)課程的人員
現(xiàn)在兩個(gè)數(shù)組內(nèi)容都需要保存,所以使用“{"$all",[內(nèi)容1,內(nèi)容2,...]}”
> db.emp.find({"course":{"$all":["語(yǔ)文","數(shù)學(xué)"]}}).pretty();
{
"_id" : ObjectId("599129a00184ff511bf02b87"),
"name" : "劉A",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"語(yǔ)文",
"數(shù)學(xué)",
"英語(yǔ)",
"音樂",
"政治"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b88"),
"name" : "劉B",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"語(yǔ)文",
"數(shù)學(xué)"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b89"),
"name" : "劉C",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"語(yǔ)文",
"數(shù)學(xué)",
"英語(yǔ)"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b8a"),
"name" : "劉D",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"語(yǔ)文",
"數(shù)學(xué)",
"政治"
]
}
現(xiàn)在所有顯示的人員信息里面包含語(yǔ)文和數(shù)學(xué)的內(nèi)容。而如果差一個(gè)內(nèi)容的不會(huì)顯示。
雖然“$all”計(jì)算可以用于數(shù)組上,但是也可以用于一個(gè)數(shù)據(jù)的匹配上。
范例:查詢位置是“成都”的人員
> db.emp.find({"loc":{"$all":["成都"]}}).pretty();
{
"_id" : ObjectId("599108423268c8e84253be2c"),
"name" : "鄭七",
"sex" : "女",
"age" : 50,
"sal" : 4700,
"loc" : "成都"
既然在集合里面現(xiàn)在保存的是數(shù)組信息,那么數(shù)組就可以利用索引操作,使用“key.index”的方式來(lái)定義索引。
范例:查詢課程中第二個(gè)內(nèi)容(index=1,索引下標(biāo)從0開始)為數(shù)學(xué)的信息
> db.emp.find({"course.1":"數(shù)學(xué)"}).pretty();
{
"_id" : ObjectId("599129a00184ff511bf02b87"),
"name" : "劉A",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"語(yǔ)文",
"數(shù)學(xué)",
"英語(yǔ)",
"音樂",
"政治"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b88"),
"name" : "劉B",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"語(yǔ)文",
"數(shù)學(xué)"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b89"),
"name" : "劉C",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"語(yǔ)文",
"數(shù)學(xué)",
"英語(yǔ)"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b8a"),
"name" : "劉D",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"語(yǔ)文",
"數(shù)學(xué)",
"政治"
]
}
范例:要求查詢出只參加兩門課程的人員
使用“$size”來(lái)進(jìn)行數(shù)量控制
> db.emp.find({"course":{"$size":2}}).pretty();
{
"_id" : ObjectId("599129a00184ff511bf02b88"),
"name" : "劉B",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"語(yǔ)文",
"數(shù)學(xué)"
]
}
{
"_id" : ObjectId("599129a20184ff511bf02b8b"),
"name" : "劉E",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"語(yǔ)文",
"政治"
]
}
發(fā)現(xiàn)在進(jìn)行數(shù)據(jù)查詢的時(shí)候,只要內(nèi)容符合條件,數(shù)組的內(nèi)容就全備顯示出來(lái),希望控制返回的數(shù)量,可以使用“$slice”進(jìn)行控制。
范例:返回年齡為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" : [
"語(yǔ)文",
"數(shù)學(xué)"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b88"),
"name" : "劉B",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"語(yǔ)文",
"數(shù)學(xué)"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b89"),
"name" : "劉C",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"語(yǔ)文",
"數(shù)學(xué)"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b8a"),
"name" : "劉D",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"語(yǔ)文",
"數(shù)學(xué)"
]
}
{
"_id" : ObjectId("599129a20184ff511bf02b8b"),
"name" : "劉E",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"語(yǔ)文",
"政治"
]
}
現(xiàn)在只取得了前兩門的信息,那么也可以設(shè)置負(fù)數(shù)取出后兩門信息。
> 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" : [
"語(yǔ)文",
"數(shù)學(xué)"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b89"),
"name" : "劉C",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"數(shù)學(xué)",
"英語(yǔ)"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b8a"),
"name" : "劉D",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"數(shù)學(xué)",
"政治"
]
}
{
"_id" : ObjectId("599129a20184ff511bf02b8b"),
"name" : "劉E",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"語(yǔ)文",
"政治"
]
}
或者只是取出中間部分的信息。
> 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" : [
"數(shù)學(xué)",
"英語(yǔ)"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b88"),
"name" : "劉B",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"數(shù)學(xué)"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b89"),
"name" : "劉C",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"數(shù)學(xué)",
"英語(yǔ)"
]
}
{
"_id" : ObjectId("599129a00184ff511bf02b8a"),
"name" : "劉D",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"數(shù)學(xué)",
"政治"
]
}
{
"_id" : ObjectId("599129a20184ff511bf02b8b"),
"name" : "劉E",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京",
"course" : [
"政治"
]
}
在此時(shí)設(shè)置的兩個(gè)數(shù)據(jù)里面,第一個(gè)數(shù)據(jù)表示開始的位置,第二個(gè)數(shù)據(jù)表示個(gè)數(shù)。
分享標(biāo)題:MongoDB之?dāng)?shù)據(jù)查詢(數(shù)組)
分享URL:
http://weahome.cn/article/igdjso.html