-
-
Save mikermcneil/483987369d54512b6104 to your computer and use it in GitHub Desktop.
/** | |
* 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); | |
}); | |
}); | |
} | |
}; | |
With this example, how we can populate one model using native ?
Is the function inside "native" run in the mongodb server? I mean, no data is sent to waterline until de callback of "toArray")?
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);
});
});
});
});
How Do I use the native() method with promise ( bluebird) I tried I could not find a way out of this? thanks in advance
@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)
});
})
}
@Rafi993
const collection = await Promise.fromCallback(Model.native);
what are the methods that collection supports? because when I do collection.stats
It works but it throws an error for collection.serverStats
what's the meaning of collection.find({}, {
name: true
})?? collection is name of our collection? or Pet.native is for name of collection?
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?
Its very useful for me..
Thanks Sir......