Vishnu Mishra
Vishnu Mishra

Reputation: 4029

How to use MongoDB in sails instead of waterline

I have a database which have approx 600000 records.
I 'm using the sails.js but when I'm fetching the data with waterline approach It takes very long time to fetch 600000 records (approx 17sec) and It has limited query i.e It doesn't have Accessing Join Tables. so I join take the result of two query and then filter the data that's by It take lot's of time.

So I decided to use MongoDB with sails instead of waterline and I'm wondering if I can somehow use the Blueprint API without being linked to a Waterline model.

How to use the MongoDB instead of the waterline?

Upvotes: 1

Views: 513

Answers (1)

myusuf
myusuf

Reputation: 12240

If you want to use the Sails models api, you can overwrite the blueprint methods in the controller.

So to overwrite, say, User Model, create the following functions in UserController.js:

find, create, update, destroy

find will override 'get api/v1/user'

create will override 'post api/v1/user'

update will override 'put api/v1/user'

destroy will override 'delete api/v1/user'

Once inside the controller, you can run a native query on Mongo like so:

In UserControllelr.js

find: function (req, res) {

  var packet = req.params.all();
  // packet now has all url and posted parameters
  User.native(function (err, UserCollection) {
    if(err) {
      //handle error
    }
    else {
      // here you can run any native mongo Query using UserCollection
      // eg:
      UserCollection.aggregate(
        {"$match": {"gender": "Male"} },
        {"$group": { "_id": "$socialNetwork", "count": {"$sum":1} } },
        function (err, results) {
          //do stuff with results
      })
    }
  })

}

Hope this helps.

Upvotes: 1

Related Questions