2013-01-22 23 views
0

在MongoDB中存儲500多個Facebook好友需要很長時間,我認爲我這麼做是錯誤的。我貼我是如何做的插入:我應該如何在Meteor Mongodb中插入一堆數據?

models.js:

Friends = new Meteor.Collection('friends'); 
Friend = { 
set : function(owner, friend) { 
    var user_id = get_user_by_uid(friend['uid']); 
    return Friends.update({uid: friend['uid'], owner: owner}, {$set:{ 
     name : friend['name'], 
     pic_square : 'https://graph.facebook.com/'+friend['uid']+'/picture?width=150&height=150', 
     pic_cover : friend['pic_cover'], 
     uid : friend['uid'], 
     likes_count : friend['likes_count'], 
     friend_count : friend['friend_count'], 
     wall_count : friend['wall_count'], 
     age : get_age(friend['birthday_date']), 
     mutual_friend_count : friend['mutual_friend_count'], 
     owner : owner, 
     user_id : user_id ? user_id['_id'] : undefined 
    }}, {upsert: true}); 
} 
} 

server.js:

// First get facebook list of friends 
friends = friends['data']['data']; 

_.each(friends, function(friend){ 
    Friend.set(user_id, friend); 
}); 

的負荷變爲高電平與2+用戶,它需要年齡在數據庫中插入。我應該在這裏改變什麼?

+0

爲什麼你使用Friends.update而不是Friends.insert? – Rahul

+0

因爲它是一個upsert! – gabrielhpugliese

+0

嘗試插入數據並查看速度是否提高。另請注意,minimongo中不支持upsert(客戶端在Meteor中實現mongodb) – Rahul

回答

4

由於兩個原因,我認爲表現不好。

首先,您在客戶端上遇到minimongo性能,而不是mongodb性能。 minimongo不能索引,所以upsert是昂貴的 - 它是O(n^2)數據庫大小昂貴。在數據庫更新之前,只需在模型中添加if (Meteor.isSimulation) return;語句即可。

查看一些示例代碼,瞭解如何組織代碼,因爲Friend.set(user_id, friend)應該發生在方法調用中,通常在您的model.js中定義。然後,如果它被檢測爲模擬呼叫的客戶端而不是服務器執行它,它應該逃脫。

其次,您正在使用uidowner就像一把鑰匙,而不會使它們成爲鑰匙。在您的服務器啓動代碼上,添加Friends._ensureIndex({uid:1, owner:1})

如果這些都不起作用,那麼您對Facebook的查詢可能會以某種方式限速。

查看https://stackoverflow.com/a/8805436/1757994瞭解一些關於您在收到費率限制時收到的錯誤消息的討論。

他們幾乎肯定不希望你按照自己的方式複製圖表。您可能要考慮不要複製圖表,只能根據使用情況獲取數據,因爲它很快就會變得過時。

+0

Friend.set發生在方法調用中,我會嘗試其他點BRB – gabrielhpugliese

+0

Did: 'set:function(owner,friend){var user_id = get_user_by_uid(friend ['uid']); if(Meteor.isSimulation)return; [...]並將_ensureIndex放入服務器的Meteor.startup中。需要清除集合/數據庫或其他東西? – gabrielhpugliese

+0

不,我不認爲你必須清除任何東西。如果你沒有看到性能改進,並且你確實在查詢每一行的Facebook,那麼你的速度是有限的。看到:http://stackoverflow.com/a/8805436/1757994 – DoctorPangloss

相關問題