mongodb查询以_开头的集合

Sal*_*ali 5 mongodb mongo-shell

基本上问题很简单:

如何对以 _ 开头的集合发出查询?

例如,如果我有 2 个集合testand _test,并且我正在尝试db.test.findOne()并且db._test.findOne()mongoshell第一个集合中按预期工作,而第二个告诉我TypeError: db._testhas no properties (shell):1

Wha*_*ool 5

将它放在引号中并使用 getCollection 方法。看这篇文章

示例 要创建集合 _foo 并插入 { a : 1 } 文档,请使用以下操作:

db.getCollection("_foo").insert( { a : 1 } )
Run Code Online (Sandbox Code Playgroud)

要执行查询,请使用 find() 方法,如下所示:

db.getCollection("_foo").find()
Run Code Online (Sandbox Code Playgroud)