mongodb $lookup 中的 $match 操作来比较 objectId 没有按预期工作 [英] mongodb $match operation in $lookup to compare objectId's not working as expected

查看:214
本文介绍了mongodb $lookup 中的 $match 操作来比较 objectId 没有按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出用户是否喜欢某个产品,同时使用 $lookup 操作从集合中获取产品以搜索喜欢集合中的喜欢,该集合包含两个字段 productId(likedProduct) 和 userID(who喜欢这个产品)productId 和 userId 都是 ObjectId 类型这是我的产品架构

I am trying to find whether a user is liked a product or not while fetching the products from a collection using $lookup operation to search for like which is in a likes collection consists of two fields productId(likedProduct) and userID(who liked the product) both productId and userId are of type ObjectId this is my schema for products

const products = new mongoose.Schema({
created: {
    type: Date,
    default: Date.now()
},
images: [{
    type: String
}]
});

喜欢架构

new Schema({
userId: {
    type: Schema.Types.ObjectId,
    required: true,
    index: true
},
productId: {
    type: Schema.Types.ObjectId,
    required: true,
    index: true,
    ref: 'products'
},
created: {
    type: Date,
    default: Date.now()
}
});

我尝试使用 $lookup 运算符作为

I have tried using $lookup operator as

const { user } = req;
productsModels.aggregate([
            { $sort: { '_id': -1 } },
            { $limit: 10 },
            {
                $lookup: {
                    from: 'likes',
                    pipeline: [
                        {
                            $match: {
                                'productId': '$_id',
                                'userId':user.id
                            }
                        }
                    ],
                    as: 'liked'
                }
            },
        ]);}

但即使用户喜欢该产品,它也没有给我预期的结果我被喜欢为一个空数组!

but it's not giving me the expected result even though the user has liked the product and I got liked as an empty array!

{
created: "2019-08-30T10:46:53.692Z"
images: ["8f3447bb-8bb8-4c5a-acde-bcdc1b729c09"]
liked: []
price: 9
title: "developer"
userId: "5d68fea345353e1748cf7a10"
__v: 0
_id: "5d68fec745353e1748cf7a12"}

提前致谢

推荐答案

试试这个

const { user } = req;

productsModels.aggregate([
            { $sort: { '_id': -1 } },
            { $limit: 10 },
            {
                $lookup: {
                    from: 'likes',
                    let: {productId:"$_id"},
                    pipeline: [
                        {
                            $match: {
                                $expr:{$eq:['$_id', '$$productId']}},
                                'userId': mongoose.Type.Object(user.id)
                            }
                        }
                    ],
                    as: 'liked'
                }
            },
        ]);}

在您的查询中缺少两件事

In your query two things where missing

1) 将 userid 转换为 mongo 对象 id 所以我们使用了 mongoose.Types.ObjectId

1) Converting userid to mongo object id so we used mongoose.Types.ObjectId

2) 您不能在内部管道中直接使用外部集合字段,为此您创建了临时变量,因此我们使用 let 声明并与内部字段匹配,我们需要使用 $expr

2) You can't use outer collection field directly in inner pipeline for this you have create temp variable so we used let to declare and to match with internal field we need to use $expr

这篇关于mongodb $lookup 中的 $match 操作来比较 objectId 没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆