Live query allows you to create a tailable cursor on any collection in the document database. There is no requirement for the collection to be capped. Tailable queries support all operators including sorts.
A tailable query with the sort operator must have a corresponding index or compound index on the sort field(s)
Query tailing is intended to replace legacy approaches to building real time apps such as polling or tailing the replication log of the database.
The example provided below uses the standard mongodb node.js driver. If you are using a different driver, refer to the driver reference for tailing a capped collection.
When a document is inserted which matches the query document of any of the pending cursors, the output document shown below should be expected by the listening client
When a document is updated which matches the query document of any of the pending cursors, the output document shown below should be expected by the listening client
When a document is deleted which matches the query document of any of the pending cursors, the output document shown below should be expected by the listening client
Using the sort operator together with a tailable cursor on the 10gen sponsored node.js driver (2.2.5) throws an exception. To work around this you can use the addQueryModifier on the cursor to specify the sort criteria for the query as seen above. If you are using a legacy driver which allows you to use the sort method directly on a tailable cursor, you should defer to using that instead.
The driver will kill the cursor when it reads the total number of documents equal to the limit set on the query. This however is undesirable when the intent is to implement a live query system which needs to alert the client of new changes. As such you can specify the limit criteria using the propery "liveQuerySortlimit" in the comment string of the query. Note the comment parameters are encoded as a regular URL query string.
The document returned will either contain the set array attribute only or a combination of the remove and add array attribute
The set array attribute always returns in sorted order all the documents contained in the query. That is if the query was to sort on a field and limit by 15, if there are 15 documents in the collection, set is guaranteed to always contain all the 15 documents in the requested sort order. If you set the parameter "liveQuerySortSetOnly=1" in the comment of the query, the document returned by the cursor will always contain the "set" array. This is much easier to work with as for example you can simply rewrite the HTML of your web app whenever the cursor returns a document.
The add array returns documents and their positions in the list. For example if the query is to sort and limit by 15, the add array returns documents and the position of the document in the entire set. Note you will only receive the add array if the you did not set the "liveQuerySortSetOnly=1". You will only go this route if you are returning very large documents and transmitting the entire query set over the wire is inefficient. However, this is typically rare for most cases.
The remove array contains one or more documents to be removed from the query set. Note you will only receive the remove array attribute if the you did not set the "liveQuerySortSetOnly=1".
Streaming sorts require an index on the sort attribute as well as the limit clause on the query. Changes are computed incrementally but can be cpu intensive in cases when the cardinality of the sort index is high and a filter conditon was specified with the query.