2

我正在使用 MongoDB Enterprise,MongoDB shell 版本:3.2.5

我有一个 db = mydb 和一个 collections = ['events', 'events__2015-12-01', 'events__2015-11-01']

我有一个 python/pymongo 脚本,我可以在其中连接到每个文档,但是在 mongo shell 中我无法连接到过时的集合?

换句话说

mongodb> use mydb 
switched to db mydb
mongodb> db.event
mydb/event
mongodb> db.event__2015-12-01
NaN
mongodb> db.event__2015-11-01
NaN
mongodb> show collections
event
event__2015-11-01
event__2015-12-01

为什么这会发生在外壳中?

编辑:

注意:进一步检查Mongo 文档。明确指出带有特殊字符的集合只能通过 getCollection() 方法访问

4

2 回答 2

1

当您编写db.event__2015-12-01时,event__2015-12-01您的集合对象与集合数组或列表(Python)中的元素完全不同。

要从字符串创建集合对象,您需要使用getCollection()shell 中的get_collection()方法和 Python 脚本中的方法。您也可以使用[]运算符。

于 2016-12-21T12:42:42.813 回答
0

您可以使用以下任一语法来访问带有连字符名称的集合:

db['event__2015-12-01']

或者

db.getCollection('event__2015-12-01')
于 2016-12-21T12:25:17.533 回答