首页 > 解决方案 > MongoError: $match with $text 只允许作为第一个管道阶段

问题描述

我有 MongoDb 与搜索文本聚合。但是 mongoDb throw Execption, idk 也许我的聚合是错误的?或者我不能使用 $match 超过 1 次?抱歉,我是 MongoDB 新手

const current_date = new Date()
var two_days_ago = current_date
two_days_ago.setDate(current_date.getDate() - 2)

return Post.aggregate([
    { $match: { editorChoice: false }},
    { $match: { $text: { $search: category } } },
    { $match: { $expr: { $and: [{ $gte: ["$createdAt", two_days_ago ] }, { $lte: ["$createdAt", current_date ] }] } } },
    { $sort: {'likeCount': -1} },
    { $limit: 80 },
    { $lookup: {
        from: 'likes', 
        let: { postUid: '$_id' }, 
        pipeline: [ { 
            $match: {
                $expr: { 
                    $and: [ 
                    { $eq: ['$user_id', mongose.Types.ObjectId(user_id)] }, 
                    { $eq: ['$post_id', '$$postUid'] } ] 
                } 
            } 
        } ],
        as: 'likes' } },
    { $addFields: { isLike: { $eq: ['$likeCount', 1] } } },
    { $lookup: {
        from: 'locations',
        localField: 'location',
        foreignField: '_id',
        as: 'location' } },
    { $unwind: {
        path: '$location',
        preserveNullAndEmptyArrays: true }},
    { $lookup: {
        from: 'sticker_images',
        localField: 'sticker',
        foreignField: '_id',
        as: 'sticker' } },
    { $unwind: {
        path: '$sticker',
        preserveNullAndEmptyArrays: true }},
    { $lookup: {
        from: 'prangko_images',
        localField: 'prangko',
        foreignField: '_id',
        as: 'prangko' } },
    { $unwind: {
        path: '$prangko',
        preserveNullAndEmptyArrays: true }},
    { $project: aggregate_project }
]).exec()

但我有类似的文本搜索聚合但没有这个

{ $match: { $expr: { $and: [{ $gte: ["$createdAt", two_days_ago ] }, { $lte: ["$createdAt", current_date ] }] } } },

它是我的 $match 错误还是关于 Date 变量的东西?

标签: mongodbmongoosemongodb-query

解决方案


试试这样

return Post.aggregate([
    { $match: { $text: { $search: category } } },
    { $match: { editorChoice: false }},
    { $match: { $expr: { $and: [{ $gte: ["$createdAt", two_days_ago ] }, { $lte: ["$createdAt", current_date ] }] } } },
    { $sort: {'likeCount': -1} },
    { $limit: 80 },
    { $lookup: {
        from: 'likes', 
        let: { postUid: '$_id' }, 
        pipeline: [ { 
            $match: {
                $expr: { 
                    $and: [ 
                    { $eq: ['$user_id', mongose.Types.ObjectId(user_id)] }, 
                    { $eq: ['$post_id', '$$postUid'] } ] 
                } 
            } 
        } ],
        as: 'likes' } },
    { $addFields: { isLike: { $eq: ['$likeCount', 1] } } },
    { $lookup: {
        from: 'locations',
        localField: 'location',
        foreignField: '_id',
        as: 'location' } },
    { $unwind: {
        path: '$location',
        preserveNullAndEmptyArrays: true }},
    { $lookup: {
        from: 'sticker_images',
        localField: 'sticker',
        foreignField: '_id',
        as: 'sticker' } },
    { $unwind: {
        path: '$sticker',
        preserveNullAndEmptyArrays: true }},
    { $lookup: {
        from: 'prangko_images',
        localField: 'prangko',
        foreignField: '_id',
        as: 'prangko' } },
    { $unwind: {
        path: '$prangko',
        preserveNullAndEmptyArrays: true }},
    { $project: aggregate_project }
]).exec()

推荐阅读