我有两个应用程序正在使用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(),以查看是否得到了不同的结果?
发布于 2019-09-25 02:19:47
我可以看到两者之间的一个区别是,一个是在listCollections: 1.0中传递,另一个是在listCollections: 1中传递。
listCollections command的值参数与此无关--在构造BSON文档以作为参数传递给runCommand时,创建键/值对是存在的,但listCollections不使用。
以下所有内容都是等价的(尽管数值具有更大的语义意义):
db.runCommand({'listCollections': 1})
db.runCommand({'listCollections': 1.0})
db.runCommand({'listCollections': 'foo'})如果您使用的是MongoDB 4.0+,则文档格式对于提供其他可选字段(如filter和nameOnly )可能很有用。
与其在shell中将文档作为runCommand()的第一个参数传递,您还可以使用“提供命令名”来运行默认选项:
db.runCommand('listCollections')是否可以将上面的日志输出转换为db.runCommand(),以查看是否得到了不同的结果?
mongo shell中与日志输出等效的命令是:
use datalake
db.getMongo().setReadPref('secondaryPreferred')
db.runCommand({'listCollections': 1})
db.runCommand({'listCollections': 1.0})listCollections查询将以相同的方式处理。但是,由于您使用的是secondaryPreferred读取首选项,所以当两个应用程序同时运行查询时,可能会从副本集的不同成员返回此查询。通常这不会是一个问题,但是如果您的用例需要很强的一致性,则应该使用primary读取首选项。
https://stackoverflow.com/questions/58080329
复制相似问题