【问题标题】:MongoError: $match with $text is only allowed as the first pipeline stageMongoError: $match with $text 只允许作为第一个管道阶段
【发布时间】:2020-02-20 13:15:26
【问题描述】:

我有 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 变量有关?

【问题讨论】:

  • 您只能使用 $text 作为第一个管道 docs.mongodb.com/manual/reference/operator/query/text/… 将前 2 个 $match 替换为 {$match:{editorChoice: false,$text: { $search: category }}} 然后试试
  • 你也不需要像上面的3一样添加多个匹配,你可以像上面的评论一样将它们组合成一个
  • 该错误已修复,但数据未显示任何内容。我的日期错了吗?
  • 如果您现在控制台,将 two_days_ago 替换为 two_days_ago=new Date(new Date().setDate(current_date.getDate() - 2)) 两个日期相同
  • 写出关于mongodb的答案。所以我可以接受答案。 @sushantmehta

标签: mongodb mongoose mongodb-query


【解决方案1】:

这样试试

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

【讨论】:

  • 一些强调方法/问题的解释或简短摘要总是有帮助的
  • 在使用 $text 和 $mach 聚合时,$mach 和 $text 搜索应该首先在聚合管道中,否则它将不会运行
猜你喜欢
  • 2011-11-01
  • 2014-05-26
  • 2017-07-17
  • 2014-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多