首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mongodb db.runCommand调试

Mongodb db.runCommand调试
EN

Stack Overflow用户
提问于 2019-09-24 12:26:41
回答 1查看 907关注 0票数 0

我有两个应用程序正在使用listCollections获取信息。两者都在通过C客户机,但它们使用的版本不同。在我的Mongodb输出中,我可以看到它们在服务器上运行的语句略有不同:-

2019-09-22T04:24:31.707+0000我命令conn9命令datalake$cmd命令: listCollections { listCollections: 1,$readPreference:{ mode:"secondaryPreferred“},$db:"test”} numYields:0 reslen:333锁:{ Global:{ r: 2} },数据库:{ acquireCount:{ r: 1} },集合:{ acquireCount:{ r: 1} protocol:op_query 0ms

$cmd命令: conn12 { listCollections: listCollections : 1.0,$readPreference:{listCollections:"secondaryPreferred“},$db:"test”} numYields:0 reslen:333锁:{ acquireCount:{ r: 2} },数据库:{ acquireCount:{ r: 1} },集合:{ acquireCount:{ r: 1} protocol:op_query 0ms

我可以看到两者之间的一个区别是,一个是在listCollections: 1.0中传递,另一个是在listCollections: 1中传递。

有任何方法可以将上面的日志输出转换为db.runCommand(),以查看是否得到了不同的结果?

EN

回答 1

Stack Overflow用户

发布于 2019-09-25 02:19:47

我可以看到两者之间的一个区别是,一个是在listCollections: 1.0中传递,另一个是在listCollections: 1中传递。

listCollections command的值参数与此无关--在构造BSON文档以作为参数传递给runCommand时,创建键/值对是存在的,但listCollections不使用。

以下所有内容都是等价的(尽管数值具有更大的语义意义):

代码语言:javascript
复制
db.runCommand({'listCollections': 1})
db.runCommand({'listCollections': 1.0})
db.runCommand({'listCollections': 'foo'})

如果您使用的是MongoDB 4.0+,则文档格式对于提供其他可选字段(如filternameOnly )可能很有用。

与其在shell中将文档作为runCommand()的第一个参数传递,您还可以使用“提供命令名”来运行默认选项:

代码语言:javascript
复制
db.runCommand('listCollections')

是否可以将上面的日志输出转换为db.runCommand(),以查看是否得到了不同的结果?

mongo shell中与日志输出等效的命令是:

代码语言:javascript
复制
use datalake
db.getMongo().setReadPref('secondaryPreferred')
db.runCommand({'listCollections': 1})
db.runCommand({'listCollections': 1.0})

listCollections查询将以相同的方式处理。但是,由于您使用的是secondaryPreferred读取首选项,所以当两个应用程序同时运行查询时,可能会从副本集的不同成员返回此查询。通常这不会是一个问题,但是如果您的用例需要很强的一致性,则应该使用primary读取首选项。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58080329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档