These are chat archives for Automattic/mongoose

8th
May 2018
Arun Gadag
@arun-awnics
May 08 2018 04:55
Filter would work but that is not efficient
Isn't there any other way apart from filter? @jplew
Kev
@lineus
May 08 2018 10:20
@arun-awnics I wouldn't expect where to return null for documents that don't match, for example
can you share your query/schema?
unless no docs match the query
Arun Gadag
@arun-awnics
May 08 2018 10:22
Yes definitely
async media(receiverId, page, size, callback) {
        var promises = [];
        var groupMessageMaps = await GroupMessageMap.find({ groupId: receiverId.toString() });
        groupMessageMaps.map((groupMessageMap) => {
            var message = Message.findOne({ $and: [{ _id: groupMessageMap.messageId }, { 'type': { $in: ['image', 'video', 'doc'] } }] });
            promises.push(message); //messages which don't match return null value here
        });
        var messages = await Promise.all(promises);
        messages = this.filterMessage(messages).reverse(); //removes all the null messages
        if (messages.length < size) {
            callback(messages);
        } else {
            messages = messages.splice(messages.length - size);
            callback(messages);
        }
    }

    filterMessage(messages) {
        var index = -1;
        var arr_length = messages ? messages.length : 0;
        var resIndex = -1;
        var result = [];
        while (++index < arr_length) {
            var message = messages[index];
            if (message) {
                result[++resIndex] = message;
            }
        }
        return result;
    }
@lineus If you remember this code was suggested by you :P
Kev
@lineus
May 08 2018 10:36
does the filterMessage function work as you intended @arun-awnics ?
Arun Gadag
@arun-awnics
May 08 2018 10:38
@lineus Yes. It removes all the null messages