MongoDB Shell基本操作(二) - 查询

1、插入测试数据

> use mydb  
switched to db mydb  
> user1 = {FName:"Test",LName:"User",Age:30,Gender:"M",Country:"US"}  
{  
        "FName" : "Test",  
        "LName" : "User",  
        "Age" : 30,  
        "Gender" : "M",  
        "Country" : "US"  
}  
> user2 = {Name:"Test USer",Age:45,Gender:"F",Country:"US"}  
{ "Name" : "Test USer", "Age" : 45, "Gender" : "F", "Country" : "US" }  
> db.users.insert(user1)  
WriteResult({ "nInserted" : 1 })  
> db.users.insert(user2)  
WriteResult({ "nInserted" : 1 })
> for(var i = 1; i <= 20; i++) db.users.insert({"Name":"Test User"+i,"Age":10+i,"Gender":"F","Country":"India"})
WriteResult({ "nInserted" : 1 })

2、查询文档

> db.users.find()
或
> db.users.find({})

查询文档包含选择器和投影器

2.1 选择器 - 过滤文档

像SQL中的where条件或者一个用于过滤出结果的过滤器;

查询所有女性:

> db.users.find({"Gender":"F"})

查询来自印度的女性:

> db.users.find({"Gender":"F", $or:[{"Country":"India"}]})

查询来自印度或美国的女性:

> db.users.find({"Gender":"F", $or:[{"Country":"India"},{"Country":"US"}]})

聚合函数:

> db.users.find({"Gender":"F", $or:[{"Country":"India"},{"Country":"US"}]}).count()
21
> db.users.find({"Gender":"F", $or:[{"Country":"India"}]}).count()
20
> db.users.find().count()
22

2.2 投影器

查询并显示所有女性员工的名字和年龄:

> db.users.find({"Gender":"F"}, {"Name":1, "Age":1, "_id":0})
{ "Name" : "Test USer", "Age" : 45 }
{ "Name" : "Test User1", "Age" : 11 }
{ "Name" : "Test User2", "Age" : 12 }
{ "Name" : "Test User3", "Age" : 13 }
{ "Name" : "Test User4", "Age" : 14 }
{ "Name" : "Test User5", "Age" : 15 }
{ "Name" : "Test User6", "Age" : 16 }
{ "Name" : "Test User7", "Age" : 17 }
{ "Name" : "Test User8", "Age" : 18 }
{ "Name" : "Test User9", "Age" : 19 }
{ "Name" : "Test User10", "Age" : 20 }
{ "Name" : "Test User11", "Age" : 21 }
{ "Name" : "Test User12", "Age" : 22 }
{ "Name" : "Test User13", "Age" : 23 }
{ "Name" : "Test User14", "Age" : 24 }
{ "Name" : "Test User15", "Age" : 25 }
{ "Name" : "Test User16", "Age" : 26 }
{ "Name" : "Test User17", "Age" : 27 }
{ "Name" : "Test User18", "Age" : 28 }
{ "Name" : "Test User19", "Age" : 29 }
Type "it" for more

3、排序 - sort()

1用于升序排列,而-1用于降序排列;

按照年龄升序:

> db.users.find({"Gender":"F"}, {"Name":1, "Age":1, "_id":0}).sort({"Age":1})
{ "Name" : "Test User1", "Age" : 11 }
{ "Name" : "Test User2", "Age" : 12 }
{ "Name" : "Test User3", "Age" : 13 }
{ "Name" : "Test User4", "Age" : 14 }
{ "Name" : "Test User5", "Age" : 15 }
{ "Name" : "Test User6", "Age" : 16 }
{ "Name" : "Test User7", "Age" : 17 }
{ "Name" : "Test User8", "Age" : 18 }
{ "Name" : "Test User9", "Age" : 19 }
{ "Name" : "Test User10", "Age" : 20 }
{ "Name" : "Test User11", "Age" : 21 }
{ "Name" : "Test User12", "Age" : 22 }
{ "Name" : "Test User13", "Age" : 23 }
{ "Name" : "Test User14", "Age" : 24 }
{ "Name" : "Test User15", "Age" : 25 }
{ "Name" : "Test User16", "Age" : 26 }
{ "Name" : "Test User17", "Age" : 27 }
{ "Name" : "Test User18", "Age" : 28 }
{ "Name" : "Test User19", "Age" : 29 }
{ "Name" : "Test User20", "Age" : 30 }
Type "it" for more

按照名字降序并且按照年龄升序:

> db.users.find({"Gender":"F"}, {"Name":1, "Age":1, "_id":0}).sort({"Name":-1, "Age":1})
{ "Name" : "Test User9", "Age" : 19 }
{ "Name" : "Test User8", "Age" : 18 }
{ "Name" : "Test User7", "Age" : 17 }
{ "Name" : "Test User6", "Age" : 16 }
{ "Name" : "Test User5", "Age" : 15 }
{ "Name" : "Test User4", "Age" : 14 }
{ "Name" : "Test User3", "Age" : 13 }
{ "Name" : "Test User20", "Age" : 30 }
{ "Name" : "Test User2", "Age" : 12 }
{ "Name" : "Test User19", "Age" : 29 }
{ "Name" : "Test User18", "Age" : 28 }
{ "Name" : "Test User17", "Age" : 27 }
{ "Name" : "Test User16", "Age" : 26 }
{ "Name" : "Test User15", "Age" : 25 }
{ "Name" : "Test User14", "Age" : 24 }
{ "Name" : "Test User13", "Age" : 23 }
{ "Name" : "Test User12", "Age" : 22 }
{ "Name" : "Test User11", "Age" : 21 }
{ "Name" : "Test User10", "Age" : 20 }
{ "Name" : "Test User1", "Age" : 11 }
Type "it" for more

4、限制返回结果数量 - limit()

> db.users.find({"Gender":"F", $or:[{"Country":"India"},{"Country":"US"}]}).limit(2)
{ "_id" : ObjectId("5afbe1a28dda484c0d9dae6a"), "Name" : "Test USer", "Age" : 45, "Gender" : "F", "Country" : "US" }
{ "_id" : ObjectId("5afbe1b08dda484c0d9dae6b"), "Name" : "Test User1", "Age" : 11, "Gender" : "F", "Country" : "India" }

5、跳过指定数量记录 - skip()

> db.users.find({"Gender":"F", $or:[{"Country":"India"},{"Country":"US"}]}).limit(2).skip(2)
{ "_id" : ObjectId("5afbe1b08dda484c0d9dae6c"), "Name" : "Test User2", "Age" : 12, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbe1b08dda484c0d9dae6d"), "Name" : "Test User3", "Age" : 13, "Gender" : "F", "Country" : "India" }

6、返回符合条件的第一个文档 - findOne()

find()返回一个游标,而findOne()返回单个文档;

> db.users.findOne({"Gender":"F"}, {"Name":1, "Age":1})
{
        "_id" : ObjectId("5afbe1a28dda484c0d9dae6a"),
        "Name" : "Test USer",
        "Age" : 45
}
> db.users.findOne()
{
        "_id" : ObjectId("5afbe19f8dda484c0d9dae69"),
        "FName" : "Test",
        "LName" : "User",
        "Age" : 30,
        "Gender" : "M",
        "Country" : "US"
}

7、使用游标

7.1 使用while循环操作储存游标对象的变量

> var c = db.users.find({"Country":"US"})
> while(c.hasNext()) printjson(c.next())
{
        "_id" : ObjectId("5afbe19f8dda484c0d9dae69"),
        "FName" : "Test",
        "LName" : "User",
        "Age" : 30,
        "Gender" : "M",
        "Country" : "US"
}
{
        "_id" : ObjectId("5afbe1a28dda484c0d9dae6a"),
        "Name" : "Test USer",
        "Age" : 45,
        "Gender" : "F",
        "Country" : "US"
}

7.2 把储存游标对象的变量作为数组来操作

> var c = db.users.find({"Country":"US"})
> printjson(c[1])
{
        "_id" : ObjectId("5afbe1a28dda484c0d9dae6a"),
        "Name" : "Test USer",
        "Age" : 45,
        "Gender" : "F",
        "Country" : "US"
}
> c[0]
{
        "_id" : ObjectId("5afbe19f8dda484c0d9dae69"),
        "FName" : "Test",
        "LName" : "User",
        "Age" : 30,
        "Gender" : "M",
        "Country" : "US"
}

8、 explain()

从版本3.0开始,使用了一个可选的被称为verbose的参数,详细级别的模式:allPlansExecution、executionStats以及queryPlanner,默认的详细级别模式是queryPlanner;

> db.users.find({"Name":"Test User"}).explain("allPlansExecution")
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "mydb.users",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "Name" : {
                                "$eq" : "Test User"
                        }
                },
                "winningPlan" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "Name" : {
                                        "$eq" : "Test User"
                                }
                        },
                        "direction" : "forward"
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 0,
                "executionTimeMillis" : 0,
                "totalKeysExamined" : 0,
                "totalDocsExamined" : 22,
                "executionStages" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "Name" : {
                                        "$eq" : "Test User"
                                }
                        },
                        "nReturned" : 0,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 24,
                        "advanced" : 0,
                        "needTime" : 23,
                        "needYield" : 0,
                        "saveState" : 0,
                        "restoreState" : 0,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "direction" : "forward",
                        "docsExamined" : 22
                },
                "allPlansExecution" : [ ]
        },
        "serverInfo" : {
                "host" : "localhost.localdomain",
                "port" : 27017,
                "version" : "3.4.10",
                "gitVersion" : "078f28920cb24de0dd479b5ea6c66c644f6326e9"
        },
        "ok" : 1
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页