MongoDB之資料查詢(陣列)

stonebox1122發表於2017-08-29
首先在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" : [
                "政治"
        ]
}
在此時設定的兩個資料裡面,第一個資料表示開始的位置,第二個資料表示個數。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28536251/viewspace-2144296/,如需轉載,請註明出處,否則將追究法律責任。

相關文章