Home > OS >  Mongodb aggregation returns empty array even though data is there
Mongodb aggregation returns empty array even though data is there

Time:07-10

Hello guys I have been working on a project where I am building a chat app

so I have a model for chatting like this

const chatSchema = new mongoose.Schema(
    {
        participants: [
            {
                type: mongoose.Schema.Types.ObjectId,
                ref: 'User',
            },
        ],
        messages: [
            {
                from: {
                    type: mongoose.Schema.Types.ObjectId,
                    ref: 'User',
                },
                message: String,
                date: {
                    type: Date,
                    default: Date.now,
                },
                delivered: [
                    {
                        type: mongoose.Schema.Types.ObjectId,
                        ref: 'User',
                    },
                ],
                seen: [
                    {
                        type: mongoose.Schema.Types.ObjectId,
                        ref: 'User',
                    },
                ],
            },
        ],
        isGroup: {
            type: Boolean,
            default: false,
        },
        groupName: {
            type: String,
        },
        admins: [
            {
                type: mongoose.Schema.Types.ObjectId,
                ref: 'User',
            },
        ],
    },
    { timestamps: true },
);

Now to get messages from this schema I am writing the following MongoDB aggregation query

const aggregateQuery: PipelineStage[] = [
            {
                $match: findQuery,
            },
            {
                $unwind: '$messages',
            },
            {
                $lookup: {
                    from: 'users',
                    localField: 'messages.from',
                    foreignField: '_id',
                    as: 'messages.from',
                    pipeline: [
                        {
                            $project: {
                                username: 1,
                                name: 1,
                                email: 1,
                            },
                        },
                    ],
                },
            },
            {
                $unwind: '$messages.from',
            },
            {
                $sort: {
                    'messages.date': -1,
                },
            },
            {
                $group: {
                    _id: '$_id',
                    'sortedMessages': { $push: '$messages' },
                    participants: { $first: '$participants' },
                    isGroup: { $first: '$isGroup' },
                    groupName: { $first: '$groupName' },
                    admins: { $first: '$admins' },
                },
            },
            {
                $project: {
                    messages: {
                        $reverseArray: {
                            $slice: ['$sortedMessages', skip, limit],
                        },
                    },
                    groupName: 1,
                    participants: 1,
                    isGroup: 1,
                    admins: 1,
                    count: {
                        $size: '$sortedMessages',
                    },
                },
            },
            {
                $lookup: {
                    from: 'users',
                    localField: 'participants',
                    foreignField: '_id',
                    as: 'sender',
                    pipeline: [
                        {
                            $match: {
                                _id: {
                                    $ne: new mongoose.Types.ObjectId(userId),
                                },
                            },
                        },
                        {
                            $limit: 1,
                        },
                        {
                            $project: {
                                username: 1,
                                name: 1,
                                email: 1,
                            },
                        },
                    ],
                },
            },
            {
                $unwind: '$sender',
            },
        ];

Now the problem is when an array of messages has some values because whenever I create a chat document I assign an empty array inside it like this [] so when hitting this query and If the array is empty then even though the match query finds the result comes as an empty array without any data

but if the message array has something in it then data comes as expected.

I can not figure out what I am doing wrong here.

the find query goes like this

const findQuery = {
    participants: {
       $in: [new mongoose.Types.ObjectId(req.user?._id)]

}

NOTE: I checked server times userId is there.

  • Related