In MongoShell: Not able to connect to my collection, db.collection_name return NaN

旧巷老猫 提交于 2019-12-11 00:05:50

问题


I am using MongoDB Enterprise, MongoDB shell version: 3.2.5

I have a db = mydb and a collections = ['events', 'events__2015-12-01', 'events__2015-11-01']

I have a python/pymongo script where I can connect to every document but in mongo shell I cannot to connect to the dated collections?

in other words

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

why does this happen in the shell?

EDIT:

Note: On further inspection Mongo Documentations. Clearly states that collections with special characters can only be accessed via getCollection() method


回答1:


When you write db.event__2015-12-01, event__2015-12-01 is your collection object which is quite different from the elements in your collections array or list (Python).

To create a collection object from string, you need to use the getCollection() method in the shell and the get_collection() method in your Python script. You can use the [] operator as well.




回答2:


You can use either of following syntaxes to access collections with hyphenated names:

db['event__2015-12-01']

OR

db.getCollection('event__2015-12-01')


来源:https://stackoverflow.com/questions/41262707/in-mongoshell-not-able-to-connect-to-my-collection-db-collection-name-return-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!