MongoDB聚合–字符串或objectId上的$ match

时间:2019-06-04 12:10:03

标签: mongodb mongoose aggregation-framework

尝试使此查询正确无误。我需要匹配来自express作为参数传递的字符串。问题在于它可以是字符串值或ObjectId。真正的问题是Mongoose不会自动将字符串强制转换为ObjectId,因此我必须做这部分。这是我到目前为止的内容:

{
    $match: {
        $or: [
            {
                slug: data.product,
            },
            {
                _id: mongoose.Types.ObjectId.isValid(data.product)
                    ? [mongoose.Types.ObjectId(data.product)]
                    : [],
            },
        ],
    },
},
  1. /products/:product是我的路线
  2. /products/stream 有效
  3. /products/5cf4530c1b5c884fbf2298e5 失败

我的完整聚合管道如下所示:

[
    {
        $match: {
            $or: [
                {
                    slug: data.product,
                },
                {
                    _id: mongoose.Types.ObjectId.isValid(data.product)
                        ? [mongoose.Types.ObjectId(data.product)]
                        : [],
                },
            ],
        },
    },
    {
        $lookup: {
            from: 'products',
            localField: '_id',
            foreignField: '_id',
            as: 'product',
        },
    },
    {
        $lookup: {
            from: 'ratings',
            localField: '_id',
            foreignField: 'product',
            as: 'rating',
        },
    },
    {
        $lookup: {
            from: 'categories',
            localField: 'categories',
            foreignField: '_id',
            as: 'category',
        },
    },
    {
        $unwind: {
            path: '$rating',
            preserveNullAndEmptyArrays: true,
        },
    },
    {
        $unwind: {
            path: '$product',
            preserveNullAndEmptyArrays: true,
        },
    },
    {
        $unwind: {
            path: '$category',
            preserveNullAndEmptyArrays: true,
        },
    },
    {
        $group: {
            _id: '$product._id',
            product: {
                $first: {
                    _id: '$product._id',
                    name: '$product.name',
                    url: '$product.url',
                    slug: '$product.slug',
                    logo: '$product.logo',
                    tagline: '$product.tagline',
                    description: '$product.description',
                    images: '$product.images',
                    roles: '$product.roles',
                    updated: '$product.updatedAt',
                    created: '$product.createdAt',
                },
            },
            categories: {
                $addToSet: {
                    _id: '$category._id',
                    name: '$category.name',
                    image: '$category.image',
                },
            },
            ratings: {
                $first: {
                    total: {
                        $sum: 1,
                    },
                    score: {
                        $sum: '$rating.rating',
                    },
                    sentiment: {
                        $sum: '$rating.sentiment',
                    },
                },
            },
        },
    },
]

有什么想法吗?这些文档对我没有太大帮助。预先感谢!

0 个答案:

没有答案