2012-03-21 66 views
2

我在Python/PyMongo中有类似的代码(代码应该模拟内部连接以及第二个集合中的所有文档,将其放入(嵌入在列表中)并添加到Mongo中的第一个适当文档中)。想法是加载两个集合并传递两个字段(用于连接的字段的名称)和第二个集合中的所有文档具有相同的属性,如首先放入列表中,并将其添加到第一个集合中的相应文档。无法更新PyMongo中的文档

(例如,在第一个集合中,我有文件fileds“country”,“population”,第二我有“country”和“car factories”,我想把所有工厂的第一个集合清单适当的国家)

for f in first_collection_records: 
     temp=[] 
     id=f['_id'] 
     second_collection_records.rewind() 
     for s in second_collection_records: 
      if f[field_one]==s[field_two]: 
       temp.append(s) 
     f[self.__second_collection_name__]=temp 
     first_collection_records.update({"_id":id}, f, safe=True) 

,但我得到的错误“光标”对象有没有属性“更新”。我做错了什么?

+0

你可以对你想达到什么更具体的?你想更新什么样的东西?你的意思是.update({“_ id”:id},f,safe = True)? – EwyynTomato 2012-03-21 09:02:33

+0

@EwyynTomato我添加了新评论,现在可以帮我解决了吗 – Damir 2012-03-21 09:16:54

回答

5

first_collection_records是一个pymongo.cursor.Cursor对象。这是您拨打db.first_collection.find()时得到的结果。你需要调用update你原来collection对象:

# assuming your query looked something like this 
first_collection_records = db.first_collection.find() 

# your code here.... 

# your last line should reference the collection object to do the update 
db.first_collection.update({"_id":id}, f, safe=True)