Search code examples
javascriptmongodbmeteorsmartcollection

Meteor SmartCollection giving inconsistent results


On the browser JS console, News.insert({name: 'Test'}) caused {{count}} to increase from 0 to 1.

In mongo console mrt mongo, db.news.find().count() returns 1. However after adding a record via the mongo console db.news.insert({name: 'TestAgain'}), {{count}} remains at 1 while in mongo, there are 2 records now.

Question: What is causing minimongo and the mongodb console to give inconsistent results?

If I replace Meteor.SmartCollection with Meteor.Collection and reload the page, {{count} is now 2. But if I were to change it back to Meteor.SmartCollection, {{count}} goes back to 1!!

collections/news.js

News = new Meteor.SmartCollection('news');

client/views/main.html

<template name="news">
    {{ count }}
</template>

client/views/main.js

Template.news.count = function() {
    return News.find().count();
}

Using Meteor v6.6.3 with SmartCollection v0.3.2.2


Update

By Cuberto's suggestion, I have enabled Oplog on my Mongodb server.

export MONGO_URL=mongodb://192.168.1.111:27017/myDb
export OPLOG_URL=mongodb://192.168.1.111:27017/local
mrt

mongod runs with --replSet meteor and mongodb was configured with

var config = {_id: "meteor", members: [{_id: 0, host: "127.0.0.1:27017"}]}
rs.initiate(config)

The prompt in mongo also becomes meteor:PRIMARY> and db.local. does contain the collection oplog.rs.

Starting meteor, we see in the console SmartCollection charged with MongoDB Oplog.

Problem: However, nothing is retrieved when we try to do News.find() in the browser JS console. Doing the same query in mongo client returns the correct result. Switching from Meteor.SmartCollection back to Meteor.Collection allows the site to work again.

How can we troubleshoot the problem with SmartCollection?


Solution

  • Make sure you configure your MongoDB to use oplog and set the environment variables, as explained here:

    http://meteorhacks.com/lets-scale-meteor.html

    Since smart collections removes the periodic database poll, you need to use an oplog-enabled mongodb instance to make it recognize DB changes from outside meteor.