Meteor js:如果记录大约为100000

时间:2017-08-04 14:12:07

标签: javascript mongodb meteor

我正在研究meteor js,mongodb数据库中会有大量数据。目前,数据库中的消息大约为50000条。我正在提供我目前正在使用的代码。由于某种原因,应用程序花费了太多时间来从数据库呈现或加载数据。还有一件事,如果我正在做任何活动,例如。就像应用程序再次从数据库中获取消息的消息一样。

Template.messages.helper({
    linkMessages() {
        var ids = _.pluck(Meteor.user().subscription, '_id');
        var messages = Messages.find({ $or: [{ feedId: { $exists: false }, link: { $exists: true } }, { feedId: { $in: ids }, link: { $exists: true } }] }, { sort: { timestamp: 1 }, limit: Session.get("linkMessageLimit") }).fetch();
        return messages;
    }

})

在oncreate方法中调用发布

Template.roomView.onCreated(function() {
    const self = this;
    Deps.autorun(function() {
        Meteor.subscribe('messages', Session.get('currentRoom'), Session.get('textMessageLimit'), {
            onReady() {
                isReady.messages = true;
                if (scroll.needScroll) {
                    scroll.needScroll = false;
                    if (scroll.previousMessage) {
                        Client.scrollToMessageText(scroll.previousMessage);
                    }
                } else {
                    Meteor.setTimeout(function() {
                        Client.scrollChatToBottomMsg();
                    }, 1000)
                }
            }
        });

    });
});`

服务器上的发布功能:

Meteor.publish('messages', function(roomId, limit) {
    check(roomId, String);
    check(limit, Match.Integer);

        let query = { $or: [{ link: {$exists: false} }, { feedId: { $exists: false } }] };

        const thresholdMessage = Messages.findOne(query, { skip: limit, sort: { timestamp: 1 } });

        if (thresholdMessage && thresholdMessage.timestamp) {
            query.timestamp = { $gt: thresholdMessage.timestamp };
        }

        return Messages.find(query, { sort: { timestamp: -1 } });

});

1 个答案:

答案 0 :(得分:0)

It is not a good practice to allow mini-mongodb to get populated with such a huge data. Though Meteor JS is good at this too, still it will take some amount of time taking into consideration the network traffic, bandwidth etc.

Irrespective of whether it is unlimited scroll or simple pagination I would suggest you to use pagination. I have already got it accepted and it works like charm, here is the answer and entire code for pagination.

My pagination solution is server specific, so it performs good. Collection publish is limited to the limit provided from subscription.

NOTE: There is yet no such proper full fledged solution for table with search and pagination and much more facility which makes it very flexible as per our need. I suggest to create your own.

MORE INSIGHTS:

https://www.quora.com/Does-Meteor-work-well-with-large-datasets

https://forums.meteor.com/t/very-big-data-collection-in-mongodb-how-to-fetch-in-meteor-js/6571/7

https://projectricochet.com/blog/top-10-meteor-performance-problems