我插入下面的文檔中的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