Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This works in Sails v0.10.0-rc7 (Waterline v0.10.0-rc12)
/**
* PetController
*
* @description :: Server-side logic for managing pets
* @help :: See http://links.sailsjs.org/docs/controllers
*/
module.exports = {
index: function (req,res) {
Pet.native(function(err, collection) {
if (err) return res.serverError(err);
collection.find({}, {
name: true
}).toArray(function (err, results) {
if (err) return res.serverError(err);
console.log('->',results);
return res.ok(results);
});
});
}
};
@muntasirjmi

This comment has been minimized.

Copy link

@muntasirjmi muntasirjmi commented Sep 4, 2015

Its very useful for me..
Thanks Sir......

@Maxtermax

This comment has been minimized.

Copy link

@Maxtermax Maxtermax commented Sep 23, 2015

With this example, how we can populate one model using native ?

@diegopamio

This comment has been minimized.

Copy link

@diegopamio diegopamio commented Jun 2, 2016

Is the function inside "native" run in the mongodb server? I mean, no data is sent to waterline until de callback of "toArray")?

@ayyappaappana

This comment has been minimized.

Copy link

@ayyappaappana ayyappaappana commented Jun 10, 2016

Hi Mike,

getDB Method manually added in sails-mongo

I need to achieve the below code snippet. I am out of thoughts, can you help me on this. I am unable to get docs.

var m = function () {
var words = this.word;
if (words) {
for (var i = 0; i < words.length; i++) {
emit(words[i], 1);
}
}
}

    var r = function (key, values) {
        var count = 0;
        values.forEach(function (v) {
            count += v;
        });
        return count;
    }

Activity.native(function (err, collection) {
console.log("hello");
collection.mapReduce(m, r, {
out: {merge: "words_count" + "" + req.params.childid}
}, function (err, result) {
Activity.getDB(function (err, db) {
db.collection("words_count" + "
" + req.params.childid).find({}, function(err, docs){
res.ok(docs);
});

            });
        });
    });
@Rafi993

This comment has been minimized.

Copy link

@Rafi993 Rafi993 commented Aug 30, 2016

How Do I use the native() method with promise ( bluebird) I tried I could not find a way out of this? thanks in advance

@avalla

This comment has been minimized.

Copy link

@avalla avalla commented Nov 25, 2016

@Rafi993 I used this syntax:

return new Promise((resolve, reject) => {
  Pet.native((err, collection) => {
    if (err) reject(err)
    else collection.find({}, {
      name: true
    }).toArray((err, results) => {
      if (err) reject(err)
      else resolve(results)
    });
  })
}
@dilame

This comment has been minimized.

Copy link

@dilame dilame commented Aug 20, 2017

@Rafi993
const collection = await Promise.fromCallback(Model.native);

@sksbtps

This comment has been minimized.

Copy link

@sksbtps sksbtps commented Aug 23, 2017

what are the methods that collection supports? because when I do collection.stats It works but it throws an error for collection.serverStats

@MisterFredy

This comment has been minimized.

Copy link

@MisterFredy MisterFredy commented May 6, 2018

what's the meaning of collection.find({}, {
name: true
})?? collection is name of our collection? or Pet.native is for name of collection?

@cope

This comment has been minimized.

Copy link

@cope cope commented Oct 21, 2020

what's the meaning of collection.find({}, {
name: true
})?? collection is name of our collection? or Pet.native is for name of collection?

Neither. Collection is the collection instance.

If your collection is called pet, then your model is most likely Pet. Then: Pet.native((err, petCollection) => { would probably be more clear?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment