skip to Main Content

I am using mongoose ODM and have a schema which looks like this:

var banSchema = new Schema({
  userid: { type: String, required: true, unique: true },
  name: String,
  groupid: String,
  reason: String,
  timestamp: Date
});

I want to output every single user id from all documents in the collection. I am using this query to obtain the userid objects. However I cannot seem to get the full list automatically. I have to manually enter the object number as seeen below:

bot.onText(//sync/i, function (msg) {
    var fromId = msg.from.id;
    var chatId = msg.chat.id;
    if (fromId == config.sudo) {
        console.log('Sudo Confirmed And Authorized!');
        Ban.find({}, function (err, obj) {
            console.log(obj[0].userid);  // Returns A Single ID
            console.log(obj[1].toObject().userid); // Returns a different ID
            bot.sendMessage(chatId, obj[1].toObject().useridid);
        });
    } else {
        console.log('Someone Is Trying To Act Like Sudo! *sigh*');
        bot.sendMessage(chatId, 'You Are Not A Mod!');
    }
});

This however does not return a full list of id’s as I want. How could I solve this issue?

The code above is for a telegram bot which on a /sync command it should return a message with all ids from the collection.

Telegram bot API Limits

Due to the API limits, the entire output should be in a single message.

4

Answers


  1. Use this syntax

            Ban.find({}).
            select('userid').
            exec(function(err, result) {
                //result is array of userid of all document
            });
    
    Login or Signup to reply.
  2. You can use this syntax:

    Ban.find({}, 'userid', function(err, users) {
        users.forEach(function(user) { 
           console.log(user);
           bot.sendMessage(chatId, 'users n' + user);
        });
    })
    
    Login or Signup to reply.
  3. 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, 'Listnn' + fixedJoin);
                });
    
    Login or Signup to reply.
  4. 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 IDsnn' + userids.join('n'));
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search