Mongoose plugin for counting references to particular objects in collection
This mongoose plugin allows to keep track of number of references to a particular object in certain collection.
var refCount = require'mongoose-reference-count';var mongoose = require'mongoose';// define our schemavar Schema =name: Stringaddress: StringdateOfBirth: Date;// adding plugin to schemaSchemapluginrefCount;var Person = mongoosemodel'Person' Schema;var newGuy = name: 'Azamat' address: 'Sesame street' dateOfBirth: 9111;newGuysave;
Now let's make some queries to
PersonfindOnename: 'Azamat'if errconsole.logerr;console.logdata;//some additional work;
This plugin does two things:
- adds hits attribute to schema
- adds post middleware for
findOne()method on requested model
The following will be printed
_id: someIDname: 'Azamat'address: 'Sesame street'dateOfBirth: 'Jan-1-1991'hits: 1
- add reference count when object is retrieved with find() method along with other objects.