MongoError:仅将$ match与$ text用作第一个管道阶段

时间:2019-09-10 04:40:00

标签: mongodb mongoose mongodb-query

我让MongoDb与搜索文本紧密结合。 但是mongoDb抛出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变量的事情?

1 个答案:

答案 0 :(得分:0)

尝试这样

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()