{
"teachers" : [
{"name": "Lucy", "id": 3, course: "history"},
{"name": "Mark", "id": 6, "course": "maths"},
{"name": "Joan", "id": 20, course: "French"}
]
}
本文檔位於「學校」集合中。我一直在嘗試使用Mongo DB:如何將一組文檔轉換爲單個文檔?
db.school.find({teachers:{id:3}})
訪問這些文件嵌入我也試過
db.school.find({teacher.id:3})
,但我的理解是,因爲蒙戈不工作不能看的嵌入式陣列內。 因此,我想將這些嵌入式文檔變成單個文檔。也就是說,刪除嵌入和「教師」鍵,爲每位教師創建一個單獨的文檔。
最後的「學校」的收集將
{"name": "Lucy", "id": 3, "course": "history"},
{"name": "Mark", "id": 6, "course": "maths"},
{"name": "Joan", "id": 20, "course": "French"}
我想使用Python這樣做,並保存新文檔到一個集合。
編輯
這就是我想出現在:
import pymongo
import sys
connection = pymongo.Connection("mongodb://localhost", safe=True)
db = connection.hello
shows = db.school
for doc in db.school:
for indiv in "teachers":
try:
db.individual.insert(indiv)
except:
print "Unexpected error", sys.exc_info()[0]
所以,問題是...... – alexvassel
問題是;是否有可能變爲 {「teachers」:[{name:「Lucy」,id:3,course:「history」}, {name:「Mark」,id:6,course:「maths」}, {名: 「瓊」,編號:20,課程: 「法國」}]} 到 {名: 「露西」,ID:3,課程: 「歷史」}, {名:「馬克「,id:6,course:」maths「}, {name:」Joan「,id:20,course:」French「} – Julia
我不明白。你是什麼意思'可能'?用python?用mongodb控制檯還是隻是無論如何? – alexvassel