2016-04-22 40 views
2

MongoDB的文檔$cmd定義如下:

A special virtual collection that exposes MongoDB’s database commands. To use database commands, see Issue Commands.

而在Issue Commands部分:

running commands with db.runCommand() is equivalent to a special query against the $cmd collection

但是當我發出這個查詢對$cmd集合:

db.$cmd.find({isMaster: 1}) 

我出現以下錯誤:

{ "ok" : 0, "errmsg" : "bad numberToReturn (0) for $cmd type ns - can only be 1 or -1", "code" : 16979 }

無論如何,$cmd集合是什麼?以及如何正確地針對它發出查詢?

+1

可能欺騙http://stackoverflow.com/questions/28642311/ – chridam

+0

@chridam對此也沒有答案! –

+0

這不會讓你的_question_減少重複。 –

回答

0

經過一番挖掘和試驗mongo後,我發現使用findOne方法我實際上可以成功地對$cmd集合運行查詢。所以:

How can I properly issue a query against it?

問題與findOne查詢:

db.$cmd.findOne({isMaster: 1}) 

將有相同的結果db.runCommand({isMaster: 1}),這就好比是以下幾點:

{ 
     "ismaster" : true, 
     "maxBsonObjectSize" : 16777216, 
     "maxMessageSizeBytes" : 48000000, 
     "maxWriteBatchSize" : 1000, 
     "localTime" : ISODate("2016-04-22T12:26:21.350Z"), 
     "maxWireVersion" : 4, 
     "minWireVersion" : 0, 
     "ok" : 1 
} 
相關問題