Mongodb 为什么名称以下划线开头的集合会受到不同的处理?

Mongodb 为什么名称以下划线开头的集合会受到不同的处理?,mongodb,Mongodb,集合的名称必须以字母或下划线开头 那么为什么第一个有效,而最后两个无效呢?谢谢 > db.getCollection("_20160712").find() { "_id" : ObjectId("57a38e4991c3b3a393e9be2b"), "dimension_id" : 2, "attribute" : "good", "hour" : "20160712_06", "frequency_count" : 100 } > db._20160712.find() 201

集合的名称必须以字母或下划线开头

那么为什么第一个有效,而最后两个无效呢?谢谢

> db.getCollection("_20160712").find()
{ "_id" : ObjectId("57a38e4991c3b3a393e9be2b"), "dimension_id" : 2, "attribute" : "good", "hour" : "20160712_06", "frequency_count" : 100 }
> db._20160712.find()
2016-08-04T14:53:56.963-0400 E QUERY    [thread1] TypeError: db._20160712 is undefined :
@(shell):1:1
> db['_20160712'].stats()
2016-08-04T14:52:43.964-0400 E QUERY    [thread1] TypeError: db._20160712 is undefined :
@(shell):1:1

如果集合名称包含特殊字符,如下划线字符,则要访问集合,请使用mongo shell中的db.getCollection()方法或驱动程序的类似方法

根据
db.getCollection(name)
方法

返回功能等同于使用db.collectionName语法的集合对象。该方法对于名称可能与shell本身交互的集合非常有用,例如以开头的名称或与数据库shell方法匹配的名称

因此,实际上,您可以使用包含下划线(不在第一个位置上)的名称访问集合,而无需使用
db.getCollection()
方法

下面的示例在mongo Shell中有效

db.collection_.find();
db.collection_1.find();
只有当下划线位于第一个位置时,才能使用
db.getCollection()
方法访问mongo Shell中的集合


没有区别对待,这只是mongo Shell的工作方式。

在倒数第二段中,应将db.getCollection()改为db.getConnection()