91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MongoDB之數據查詢(數組)

發布時間:2020-08-12 17:45:23 來源:ITPUB博客 閱讀:132 作者:stonebox1122 欄目:關系型數據庫
首先在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" : [
                "政治"
        ]
}
在此時設置的兩個數據里面,第一個數據表示開始的位置,第二個數據表示個數。
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

汾阳市| 双柏县| 芒康县| 盐池县| 夏河县| 六盘水市| 东源县| 东阳市| 瓦房店市| 南岸区| 斗六市| 南江县| 台安县| 宁远县| 伊通| 博野县| 泰安市| 宜川县| 乌拉特中旗| 高唐县| 和平县| 白水县| 西和县| 湾仔区| 南投市| 雷波县| 海盐县| 清镇市| 花莲市| 开江县| 肥城市| 汝城县| 凤城市| 恩平市| 武清区| 新丰县| 大埔区| 娄底市| 收藏| 湖州市| 绿春县|