MongoDB之資料查詢(關係運算)

stonebox1122發表於2017-08-29
在MongoDB裡面支援的關係查詢操作:大於($gt),小於($lt),大於等於($gte),小於等於($lte),不等於($ne),等於(key:value,$eq)。

範例:定義一個員工資訊集合
db.emp.drop();
db.emp.insert({"name":"趙一","sex":"男","age":30,"sal":1000,"loc":"北京"});
db.emp.insert({"name":"錢二","sex":"女","age":22,"sal":5000,"loc":"上海"});
db.emp.insert({"name":"孫三","sex":"男","age":40,"sal":2000,"loc":"深圳"});
db.emp.insert({"name":"李四","sex":"女","age":30,"sal":7000,"loc":"北京"});
db.emp.insert({"name":"週五","sex":"女","age":30,"sal":6400,"loc":"北京"});
db.emp.insert({"name":"吳六","sex":"男","age":30,"sal":2500,"loc":"重慶"});
db.emp.insert({"name":"鄭七","sex":"女","age":50,"sal":4700,"loc":"成都"});
db.emp.insert({"name":"王八","sex":"男","age":35,"sal":8000,"loc":"北京"});

> 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" : "北京"
}

範例:查詢姓名為孫三的資訊
> db.emp.find({"name":"孫三"}).pretty();
{
        "_id" : ObjectId("599108423268c8e84253be28"),
        "name" : "孫三",
        "sex" : "男",
        "age" : 40,
        "sal" : 2000,
        "loc" : "深圳"
}

範例:查詢性別為男的人員資訊
> db.emp.find({"sex":"男"}).pretty();
{
        "_id" : ObjectId("599108423268c8e84253be26"),
        "name" : "趙一",
        "sex" : "男",
        "age" : 30,
        "sal" : 1000,
        "loc" : "北京"
}
{
        "_id" : ObjectId("599108423268c8e84253be28"),
        "name" : "孫三",
        "sex" : "男",
        "age" : 40,
        "sal" : 2000,
        "loc" : "深圳"
}
{
        "_id" : ObjectId("599108423268c8e84253be2b"),
        "name" : "吳六",
        "sex" : "男",
        "age" : 30,
        "sal" : 2500,
        "loc" : "重慶"
}
{
        "_id" : ObjectId("599108433268c8e84253be2d"),
        "name" : "王八",
        "sex" : "男",
        "age" : 35,
        "sal" : 8000,
        "loc" : "北京"
}

範例:查詢年齡大於30的人員
> db.emp.find({"age":{"$gt":30}}).pretty();
{
        "_id" : ObjectId("599108423268c8e84253be28"),
        "name" : "孫三",
        "sex" : "男",
        "age" : 40,
        "sal" : 2000,
        "loc" : "深圳"
}
{
        "_id" : ObjectId("599108423268c8e84253be2c"),
        "name" : "鄭七",
        "sex" : "女",
        "age" : 50,
        "sal" : 4700,
        "loc" : "成都"
}
{
        "_id" : ObjectId("599108433268c8e84253be2d"),
        "name" : "王八",
        "sex" : "男",
        "age" : 35,
        "sal" : 8000,
        "loc" : "北京"
}

範例:查詢薪水大於等於4700的人員
> db.emp.find({"sal":{"$gte":4700}}).pretty();
{
        "_id" : ObjectId("599108423268c8e84253be27"),
        "name" : "錢二",
        "sex" : "女",
        "age" : 22,
        "sal" : 5000,
        "loc" : "上海"
}
{
        "_id" : ObjectId("599108423268c8e84253be29"),
        "name" : "李四",
        "sex" : "女",
        "age" : 30,
        "sal" : 7000,
        "loc" : "北京"
}
{
        "_id" : ObjectId("599108423268c8e84253be2a"),
        "name" : "週五",
        "sex" : "女",
        "age" : 30,
        "sal" : 6400,
        "loc" : "北京"
}
{
        "_id" : ObjectId("599108423268c8e84253be2c"),
        "name" : "鄭七",
        "sex" : "女",
        "age" : 50,
        "sal" : 4700,
        "loc" : "成都"
}
{
        "_id" : ObjectId("599108433268c8e84253be2d"),
        "name" : "王八",
        "sex" : "男",
        "age" : 35,
        "sal" : 8000,
        "loc" : "北京"
}

範例:查詢姓名不是孫三的資訊
> db.emp.find({"name":{"$ne":"孫三"}}).pretty();
{
        "_id" : ObjectId("599108423268c8e84253be26"),
        "name" : "趙一",
        "sex" : "男",
        "age" : 30,
        "sal" : 1000,
        "loc" : "北京"
}
{
        "_id" : ObjectId("599108423268c8e84253be27"),
        "name" : "錢二",
        "sex" : "女",
        "age" : 22,
        "sal" : 5000,
        "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" : "北京"
此時與之前最大的區別就在於,在一個JSON結構裡面需要定義其他的JSON結構,並且這種風格在進行程式操作的時候依然如此。

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

相關文章