M102: MongoDB for DBAs - Final Exam

Q1

6

Q2

MongoDB preserves the order of writes in a collection in its consistency model. In this problem, 27003's oplog was effectively a "fork" and to preserve write ordering a rollback was necessary during 27003's recovery phase.

Q3

It contains 3 documents.

Q4

233

Q5

(true)
We can create an index to make the following query fast/faster:
db.postings.find( { "comments.flagged" : true } )

(false)
One way to assure people vote at most once per posting is to use this form of update:
db.postings.update(
  { _id: . . . },
  { $inc : {votes:1}, $push : {voters:'joe'} }
);
combined with an index on { voters : 1 } which has a unique key constraint.

(true)
One way to assure people vote at most once per posting is to use this form of update:
db.postings.update(
  { _id: . . . , voters:{$ne:'joe'} },
  { $inc : {votes:1}, $push : {voters:'joe'} } );

Q6

(true) MongoDB has a data type for binary data.
(true) MongoDB supports atomic operations on individual documents.
(false) MongoDB allows you to choose the storage engine separately for each collection on your mongod.
(false) MongoDB (v3.0) supports transactions spanning multiple documents, if those documents all reside on the same shard.

Q7

(false) MongoDB is "multi-master" -- you can write anywhere, anytime.
(true) MongoDB supports reads from slaves/secondaries that are in remote locations.
(false) Most MongoDB queries involve javascript execution on the database server(s).

Q8

mongod30 --dbpath c --configsvr --fork --logpath c.log

mongorestore30 -h localhost:27019 gene_backup/config_server


mongo30 localhost:27019/config

db.chunks.find().sort({_id:1}).next().lastmodEpoch.getTimestamp().toUTCString().substr(20,6)
39:15

Q9

mongo30 localhost:27019/config

configsvr> db.shards.find()
{ "_id" : "s1", "host" : "s1/genome_svr1:27501,genome_svr2:27502,genome_svr2:27503" }
{ "_id" : "s2", "host" : "s2/genome_svr4:27601,genome_svr5:27602,genome_svr5:27603" }

configsvr> db.shards.remove({})
WriteResult({ "nRemoved" : 2 })
configsvr> db.shards.insert({ "_id" : "s1", "host" : "localhost:27501" })
WriteResult({ "nInserted" : 1 })
configsvr> db.shards.insert({ "_id" : "s2", "host" : "localhost:27601" })
WriteResult({ "nInserted" : 1 })
configsvr> db.shards.find()
{ "_id" : "s1", "host" : "localhost:27501" }
{ "_id" : "s2", "host" : "localhost:27601" }

# 重启 config server
mongod30 --dbpath c --configsvr --fork --logpath c.log


mongod30 --dbpath s1 --port 27501 --fork --logpath s1.log
mongod30 --dbpath s2 --port 27601 --fork --logpath s2.log

mongorestore30 -h localhost:27501 gene_backup/s1
mongorestore30 -h localhost:27601 gene_backup/s2


mongos30 --configdb localhost:27019 --fork --logpath r.log

mongo30
use snps
var x = db.elegans.aggregate( [ { $match : { N2 : "T" } } , { $group : { _id:"$N2" , n : { $sum : 1 } } } ] ).next(); print( x.n )

47664

注意:macOS Sierra 下 mongorestore s1 报错 “fatal error: MSpanList_Insert”,无法导入

Q10

use snps
db.elegans.createIndex({ N2 : 1, mutant : 1 })
db.elegans.find( { N2 : "T", mutant : "A" } ).limit( 5 ).explain( "executionStats" )

mongos> db.elegans.createIndex({ N2 : 1, mutant : 1 })
{
    "raw" : {
        "localhost:27501" : {
            "createdCollectionAutomatically" : false,
            "numIndexesBefore" : 2,
            "numIndexesAfter" : 3,
            "ok" : 1
        },
        "localhost:27601" : {
            "createdCollectionAutomatically" : false,
            "numIndexesBefore" : 2,
            "numIndexesAfter" : 3,
            "ok" : 1
        }
    },
    "ok" : 1
}
mongos> db.elegans.find( { N2 : "T", mutant : "A" } ).limit( 5 ).explain( "executionStats" )
{
    "queryPlanner" : {
        "mongosPlannerVersion" : 1,
        "winningPlan" : {
            "stage" : "SHARD_MERGE",
            "shards" : [
                {
                    "shardName" : "s1",
                    "connectionString" : "localhost:27501",
                    "serverInfo" : {
                        "host" : "myhost",
                        "port" : 27501,
                        "version" : "3.0.12",
                        "gitVersion" : "33934938e0e95d534cebbaff656cde916b9c3573"
                    },
                    "plannerVersion" : 1,
                    "namespace" : "snps.elegans",
                    "indexFilterSet" : false,
                    "parsedQuery" : {
                        "$and" : [
                            {
                                "N2" : {
                                    "$eq" : "T"
                                }
                            },
                            {
                                "mutant" : {
                                    "$eq" : "A"
                                }
                            }
                        ]
                    },
                    "winningPlan" : {
                        "stage" : "LIMIT",
                        "limitAmount" : 0,
                        "inputStage" : {
                            "stage" : "KEEP_MUTATIONS",
                            "inputStage" : {
                                "stage" : "SHARDING_FILTER",
                                "inputStage" : {
                                    "stage" : "FETCH",
                                    "inputStage" : {
                                        "stage" : "IXSCAN",
                                        "keyPattern" : {
                                            "N2" : 1,
                                            "mutant" : 1
                                        },
                                        "indexName" : "N2_1_mutant_1",
                                        "isMultiKey" : false,
                                        "direction" : "forward",
                                        "indexBounds" : {
                                            "N2" : [
                                                "[\"T\", \"T\"]"
                                            ],
                                            "mutant" : [
                                                "[\"A\", \"A\"]"
                                            ]
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "rejectedPlans" : [ ]
                },
                {
                    "shardName" : "s2",
                    "connectionString" : "localhost:27601",
                    "serverInfo" : {
                        "host" : "myhost",
                        "port" : 27601,
                        "version" : "3.0.12",
                        "gitVersion" : "33934938e0e95d534cebbaff656cde916b9c3573"
                    },
                    "plannerVersion" : 1,
                    "namespace" : "snps.elegans",
                    "indexFilterSet" : false,
                    "parsedQuery" : {
                        "$and" : [
                            {
                                "N2" : {
                                    "$eq" : "T"
                                }
                            },
                            {
                                "mutant" : {
                                    "$eq" : "A"
                                }
                            }
                        ]
                    },
                    "winningPlan" : {
                        "stage" : "LIMIT",
                        "limitAmount" : 3,
                        "inputStage" : {
                            "stage" : "KEEP_MUTATIONS",
                            "inputStage" : {
                                "stage" : "SHARDING_FILTER",
                                "inputStage" : {
                                    "stage" : "FETCH",
                                    "inputStage" : {
                                        "stage" : "IXSCAN",
                                        "keyPattern" : {
                                            "N2" : 1,
                                            "mutant" : 1
                                        },
                                        "indexName" : "N2_1_mutant_1",
                                        "isMultiKey" : false,
                                        "direction" : "forward",
                                        "indexBounds" : {
                                            "N2" : [
                                                "[\"T\", \"T\"]"
                                            ],
                                            "mutant" : [
                                                "[\"A\", \"A\"]"
                                            ]
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "rejectedPlans" : [ ]
                }
            ]
        }
    },
    "executionStats" : {
        "nReturned" : 7,
        "executionTimeMillis" : 1,
        "totalKeysExamined" : 8,
        "totalDocsExamined" : 8,
        "executionStages" : {
            "stage" : "SHARD_MERGE",
            "nReturned" : 7,
            "executionTimeMillis" : 1,
            "totalKeysExamined" : 8,
            "totalDocsExamined" : 8,
            "totalChildMillis" : NumberLong(0),
            "shards" : [
                {
                    "shardName" : "s1",
                    "executionSuccess" : true,
                    "executionStages" : {
                        "stage" : "LIMIT",
                        "nReturned" : 5,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 7,
                        "advanced" : 5,
                        "needTime" : 1,
                        "needFetch" : 0,
                        "saveState" : 0,
                        "restoreState" : 0,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "limitAmount" : 0,
                        "inputStage" : {
                            "stage" : "KEEP_MUTATIONS",
                            "nReturned" : 5,
                            "executionTimeMillisEstimate" : 0,
                            "works" : 6,
                            "advanced" : 5,
                            "needTime" : 1,
                            "needFetch" : 0,
                            "saveState" : 0,
                            "restoreState" : 0,
                            "isEOF" : 0,
                            "invalidates" : 0,
                            "inputStage" : {
                                "stage" : "SHARDING_FILTER",
                                "nReturned" : 5,
                                "executionTimeMillisEstimate" : 0,
                                "works" : 6,
                                "advanced" : 5,
                                "needTime" : 0,
                                "needFetch" : 0,
                                "saveState" : 0,
                                "restoreState" : 0,
                                "isEOF" : 0,
                                "invalidates" : 0,
                                "chunkSkips" : 1,
                                "inputStage" : {
                                    "stage" : "FETCH",
                                    "nReturned" : 6,
                                    "executionTimeMillisEstimate" : 0,
                                    "works" : 6,
                                    "advanced" : 6,
                                    "needTime" : 0,
                                    "needFetch" : 0,
                                    "saveState" : 0,
                                    "restoreState" : 0,
                                    "isEOF" : 0,
                                    "invalidates" : 0,
                                    "docsExamined" : 6,
                                    "alreadyHasObj" : 0,
                                    "inputStage" : {
                                        "stage" : "IXSCAN",
                                        "nReturned" : 6,
                                        "executionTimeMillisEstimate" : 0,
                                        "works" : 6,
                                        "advanced" : 6,
                                        "needTime" : 0,
                                        "needFetch" : 0,
                                        "saveState" : 0,
                                        "restoreState" : 0,
                                        "isEOF" : 0,
                                        "invalidates" : 0,
                                        "keyPattern" : {
                                            "N2" : 1,
                                            "mutant" : 1
                                        },
                                        "indexName" : "N2_1_mutant_1",
                                        "isMultiKey" : false,
                                        "direction" : "forward",
                                        "indexBounds" : {
                                            "N2" : [
                                                "[\"T\", \"T\"]"
                                            ],
                                            "mutant" : [
                                                "[\"A\", \"A\"]"
                                            ]
                                        },
                                        "keysExamined" : 6,
                                        "dupsTested" : 0,
                                        "dupsDropped" : 0,
                                        "seenInvalidated" : 0,
                                        "matchTested" : 0
                                    }
                                }
                            }
                        }
                    }
                },
                {
                    "shardName" : "s2",
                    "executionSuccess" : true,
                    "executionStages" : {
                        "stage" : "LIMIT",
                        "nReturned" : 2,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 3,
                        "advanced" : 2,
                        "needTime" : 0,
                        "needFetch" : 0,
                        "saveState" : 0,
                        "restoreState" : 0,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "limitAmount" : 3,
                        "inputStage" : {
                            "stage" : "KEEP_MUTATIONS",
                            "nReturned" : 2,
                            "executionTimeMillisEstimate" : 0,
                            "works" : 3,
                            "advanced" : 2,
                            "needTime" : 0,
                            "needFetch" : 0,
                            "saveState" : 0,
                            "restoreState" : 0,
                            "isEOF" : 1,
                            "invalidates" : 0,
                            "inputStage" : {
                                "stage" : "SHARDING_FILTER",
                                "nReturned" : 2,
                                "executionTimeMillisEstimate" : 0,
                                "works" : 3,
                                "advanced" : 2,
                                "needTime" : 0,
                                "needFetch" : 0,
                                "saveState" : 0,
                                "restoreState" : 0,
                                "isEOF" : 1,
                                "invalidates" : 0,
                                "chunkSkips" : 0,
                                "inputStage" : {
                                    "stage" : "FETCH",
                                    "nReturned" : 2,
                                    "executionTimeMillisEstimate" : 0,
                                    "works" : 3,
                                    "advanced" : 2,
                                    "needTime" : 0,
                                    "needFetch" : 0,
                                    "saveState" : 0,
                                    "restoreState" : 0,
                                    "isEOF" : 1,
                                    "invalidates" : 0,
                                    "docsExamined" : 2,
                                    "alreadyHasObj" : 0,
                                    "inputStage" : {
                                        "stage" : "IXSCAN",
                                        "nReturned" : 2,
                                        "executionTimeMillisEstimate" : 0,
                                        "works" : 3,
                                        "advanced" : 2,
                                        "needTime" : 0,
                                        "needFetch" : 0,
                                        "saveState" : 0,
                                        "restoreState" : 0,
                                        "isEOF" : 1,
                                        "invalidates" : 0,
                                        "keyPattern" : {
                                            "N2" : 1,
                                            "mutant" : 1
                                        },
                                        "indexName" : "N2_1_mutant_1",
                                        "isMultiKey" : false,
                                        "direction" : "forward",
                                        "indexBounds" : {
                                            "N2" : [
                                                "[\"T\", \"T\"]"
                                            ],
                                            "mutant" : [
                                                "[\"A\", \"A\"]"
                                            ]
                                        },
                                        "keysExamined" : 2,
                                        "dupsTested" : 0,
                                        "dupsDropped" : 0,
                                        "seenInvalidated" : 0,
                                        "matchTested" : 0
                                    }
                                }
                            }
                        }
                    }
                }
            ]
        }
    },
    "ok" : 1
}



(true) 2 shards are queried.
() No shards are queried.
() 5 documents are examined.
() 1 shard is queried.
() 7 documents are examined.
(true) 8 documents are examined.
() Thousands of documents are examined.

-eof-

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值