Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mikermcneil/483987369d54512b6104 to your computer and use it in GitHub Desktop.
Save mikermcneil/483987369d54512b6104 to your computer and use it in GitHub Desktop.
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
Copy link

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

@Maxtermax
Copy link

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

@diegopamio
Copy link

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

@ayyappaappana
Copy link

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
Copy link

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
Copy link

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
Copy link

dilame commented Aug 20, 2017

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

@sksbtps
Copy link

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
Copy link

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

@cope
Copy link

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