Search code examples
mongodbsails.jswaterline

Sails.js mongodb map reduce


I'm using sails with sails-mongo and I'm looking to perform an aggregation across a collection. I want to reuse the connection object that waterline uses, rather than create an entirely new connection to the mongodb server. Is this connection object available somewhere, and can it perform any query type?

This is predicated on the understanding that waterline/sails cannot currently do aggregations.


Solution

  • It sounds like you're looking for the .native method, which returns the raw node mongo collection instance. This allows you to use native Mongo methods. For example, if you a User model in Sails, you would do:

    User.native(function(err, userCollection) {
    
        userCollection.aggregate(...)
    
    }
    

    Docs for the native Node Mongo driver are here, including a section on how to do aggregation.

    Docs for the .native method are on the sailsjs.org.