Output all documents in mongoose
var query = Ban.find({}).select({ "userid": 1, //Add more column fields here "_id": 0 //Ensures _id is not displayed }); var arr = []; query.exec(function (err, results) { if (err) throw err; results.forEach(function (result) { arr.push(result.userid); // Add more column fields here; }); var fixedJoin =arr.join("\n"); console.log(fixed); bot.sendMessage(chatId, 'List\n\n' + fixedJoin); });
The easiest way to get all values of a particular field across all docs in the collection is to use distinct
:
Ban.distinct('userid', function (err, userids) { // userids is an array containing all userid values in the collection. // string.join into a single string for the message. bot.sendMessage(chatId, 'USER IDs\n\n' + userids.join('\n'));});