2016-04-22 56 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 
} 
相关问题