MongoDB之資料查詢(範圍運算)

stonebox1122發表於2017-08-29
“$in”:在範圍之中
"$nin":不再範圍之中

範例:查詢姓名為“趙一”、“王八”的資訊
> db.emp.find({"name":{"$in":["趙一","王八"]}}).pretty();
{
        "_id" : ObjectId("599108423268c8e84253be26"),
        "name" : "趙一",
        "sex" : "男",
        "age" : 30,
        "sal" : 1000,
        "loc" : "北京"
}
{
        "_id" : ObjectId("599108433268c8e84253be2d"),
        "name" : "王八",
        "sex" : "男",
        "age" : 35,
        "sal" : 8000,
        "loc" : "北京"
}

範例:查詢姓名不是“趙一”、“王八”的資訊
> db.emp.find({"name":{"$nin":["趙一","王八"]}}).pretty();
{
        "_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" : "成都"
}

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

相關文章