2013-05-09 92 views
0

如何在Python中使用MongoKit將對象插入到集合中?Mongokit將對象添加到集合

我明白了,你可以在模型中指定的「收集」的領域,而且可以在DB創建模型,如

user = db.Users() 

然後保存對象像

user.save() 

不過我似乎無法找到任何提及的插入功能。如果我創建一個用戶對象,現在想將其插入到特定的集合中,請說「online_users」,我該怎麼做?

回答

0

完全猜測看來我可以成功只是調用

db.online_users.insert(user) 
0

您創建一個新文檔後稱爲OnlineUser__collection__字段設置爲online_users,然後你有相關UserOnlineUsers與任何ObjectIDDBRef 。 MongoKit supports both通過 -

  • pymongo.objectid.ObjectId
  • bson.dbref.DBRef

您也可以使用任何其他字段類型的字段的list

0

我想你user對象就像

user = { 
    "one": "balabala", 
    "two": "lalala", 
    "more": "I am happy always!" 
} 

的字典這裏是我的解決方案,而不是很好,但工作:)

online_users = db.Online_users() # connecting your collection 

for item in user: 
    if item == "item you don't want": 
     continue # skip any item you don't want 
    online_users[item] = user[item] 

online_users.save() 
db.close() # close the db connection