New Mongoose QueryStream Results
I am trying to set up MongooseJS to push the entire collection (or just the newest item) when new documents are inserted into another application's collection.
I assumed QueryStream is the way to go.
However, when I start my simple application, it reads the collection once and closes it.
When I insert a new document, nothing happens (assuming the connection no longer opens and looks for new results ...?)
var Orders = db.model('orders', OrderSchema);
var stream = Orders.find().stream();
stream.on('data', function(doc){
console.log('New item!');
console.log(doc);
}).on('error', function (error){
console.log(error);
}).on('close', function () {
console.log('closed');
});
Immediately prints all items that are in the order collection, not "closed" items. Should the "stream" remain open when printing new data when changing a collection?
What I don't understand about MongooseJS QueryStream
?
Ps. my goal is to eventually emit
update the collection via socket.io
as shown here: Mongoose stream returns multiple results the first time
source to share
I found that in order for this method to work, I needed to change my collection to capped collection
:
var OrderSchema = new Mongoose.Schema({...
}, { capped: { size: 10, max: 10, autoIndexId: true }});
var Orders = db.model('orders', OrderSchema);
var stream = Orders.find().tailable().stream();
stream.on('data', function(doc){
console.log('New item!');
console.log(doc);
}).on('error', function (error){
console.log(error);
}).on('close', function () {
console.log('closed');
});
This works because I can now view it MongoDB collection
as something from the message queue that is constantly being updated.
Oddly enough, when I wrap this inside an event SocketIO
, I get the multiplicity of the same documents
, which makes me think there is still something that I am not doing exactly ...
source to share
You need a stamp (timestamp or just a prime number) so that the entire collection is not retrieved on every run. For example, if you are inserting a timestamp in the records of a collection, you can use:
var filter = { "timestamp":{"$gte":Date.now()}};
var stream = Orders.find(filter).tailable().stream();
Think of mongoDB stream as a command tail -f
in bash.
source to share