我插入下面的文档中的MongoDB内含价值:检索MongoDB的对象(使用CherryPy的框架)
@cherrypyexpose
def create(self):
client = MongoClient()
db = client.database_name
result = db.users.insert_one({
"email": "[email protected]",
"company": {
"name": "ABC Company"
}
})
我现在想找回company.name这是“ABC公司”。
@cherrypy.expose
def result(self):
client = MongoClient()
db = client.database_name
cursor = db.users.find({"email":"[email protected]"})
for document in cursor:
value = document["company.name"]
return value
我曾尝试使用下面的语法“company.name”我会做关系数据库检索此值,但我得到这个错误:"KeyError: 'company.name'"
什么是正确的语法在Mongo上检索“company.name”?
'DB = client.database_name'和'db.users.insert_one'是坏主意,因为如果db是存在的,如果集合存在,如果在重复键等。另一点是'cursor [db] [collection] [some_variable] [entry] [some values]'等于'cursor.db.collection.some_variables.find_one({'_ id':'entry'})'mongo db不允许多个集合名称(这不是一个有效的“树”方法)。光标[db_name] [collection_name]'插入,删除,更新' – dsgdfg
@dsgdfg ['cursor [db] [collection]'不等于'cursor.db.collection'](http://stackoverflow.com/questions/ 41262707/in-mongoshell-not-able-to-connect-to-my-collection-db-collection-name-return -n) – styvane
确实,“cursor.db”是静态的,对大型树来说不是有用的。否则,需要定义搜索/写入项目的许多键和值。如果更改源数据库,需要关闭并重新打开光标。 – dsgdfg