首页 > 解决方案 > mongodb 未知顶级运算符:$function nodejs

问题描述

我试图仅提取用户未选择的那些集合。这是我的代码。

return await Campaign.aggregate([
                {
                    $lookup: {
                        from:"influncer_campaign_mapping",
                        localField: "campaignId",
                        foreignField:"campaignId",
                        as: "icmapping"
                    }
                },
                {$unwind:"$icmapping"},
                {
                  $match: {$function:{
                    body: function(icmapping) {
                        if(icmapping != null || icmapping != undefined){
                            return {"icmapping.userId":{$ne:"ccdc7d1a-9cdb-4768-b8c1-0446e35576fb"}}
                        }else{ return null}
                        
                     },
                     args: [ "$icmapping" ],
                     lang: "js"

                  }
                }
            }
        ])

我的映射表

{
        influncerCampaignMappingId: { type: String, require: true },
        userId: { type: String, require: true },
        campaignId: { type: String, require: true },
        // isSelected: { type: Boolean, require: true, default: true },
        isDeleted: { type: Boolean, require: true },
        deletedDate: { type: Date, require: true },
    },

这是活动表,我在其中存储活动列表。

 {
        campaignId: { type: String, require: true },
        name: { type: String, require: true },
        image: { type: String, require: true },
        serviceName: { type: String, require: true },
        description: { type: String, require: true },
        categoryId: [{ type: String }],
        keywords: [{ type: String }]
}

实际上,我正在考虑来自活动表的活动 ID 和来自用户表的用户 ID,并将其插入映射表中。现在的目标是仅实现用户之前未选择的那些集合(列表),以便他们只能看到他们尚未选择的那些活动(列表)。

标签: node.jsmongodb

解决方案


试试这样,希望有帮助

return await Campaign.aggregate([{
                $lookup: {
                    from: "influncer_campaign_mapping",
                    localField: "campaignId",
                    foreignField: "campaignId",
                    as: "icmapping"
                }
            },
            {
                $unwind: "$icmapping"
            },
            {
                $match: {
                    $and:
    
                        [{
                            $ne: ["icmapping.userId", "ccdc7d1a-9cdb-4768-b8c1-0446e35576fb"],
                            $ne: ["icmapping.userId", null]
                        }]
                }
            }
        ])

推荐阅读