如何使用mongodb驱动程序合并mongodb节点js中的多个集合

问题描述

我有 3 个集合。我想合并这些并从合并的数据中过滤数据。

业务集合

{
    
    _id:1,"user_id": 1,"name": "Doll Shopqq","registered_phone_number": 701006522222109,"business_profile_image_url": "http://website.com/hiyup_dev/business/1611569489867_businessImage.jpeg","email": "","media_urls": ["http://website.com/hiyup_dev/business/1611569503298_3176405500.jpeg","http://website.com/hiyup_dev/business/1611569983527_3192836205.mp4"],"description": "Doll shop","products": [{ 
        "_id": 1
        "name": "Dog Biscuits","lower_price": "0.00","media_urls": ["http://website.com/hiyup_dev/product/1611569983527_3192836205.jpeg","http://website.com/hiyup_dev/product/1611569983527_3192836205.mp4"],"higher_price": "0.00","description": "Biscuits",},{
        "_id": 2,"name": "Dog Biscuits-1","media_urls": ["http://website.com/hiyup_dev/product/1611569983527_3192836205.jpeg"],"description": "Biscuits-1",}],"status": 1,"country_code": ""
}

优惠集合

{ 
    "_id": 1,"name": "offer name 1","media_urls": ["http://website.com/hiyup_dev/offer/1611569983527_3192836205.jpeg","http://website.com/hiyup_dev/offer/1611569983527_3192836205.mp4"],{
    "_id": 2,"name": "offr name2","media_urls": ["http://website.com/hiyup_dev/offer/1611569983527_3192836205.jpeg"],}

产品请求集合

   { 
    "_id": 1,"name": "request  name 1","media_urls": ["http://website.com/hiyup_dev/request/1611569983527_3192836205.jpeg","http://website.com/hiyup_dev/request/1611569983527_3192836205.mp4"],"name": "request name2",}

我需要从业务集合中获取在 products.media_urls 中有视频的产品,同样从 offer、product_request 集合中获取我想获取在 media_urls 中有视频的项目。 我想从产品、优惠、product_request 中获取商品,它们的 media_url 数组中有视频。

我想组合这些集合并过滤只有视频的 media_urls。 对于单个集合,我使用正则表达式进行了过滤。

但我无法组合多个集合。 当我使用放松时。重复数据即将到来。

我的预期输出

{
    "_id": 2,//or some other key name like product_id
    **"type": "products"**
    "name": "Dog Biscuits-1","media_urls": [
    "http://website.com/hiyup_dev/product/1611569983527_3192836205.mp4"],{
    "_id": 1,//or some other key name
    "type": "offer"
    "name": "offer name 1","media_urls": [
    "http://website.com/hiyup_dev/offer/1611569983527_3192836205.mp4"],//or some other key name
    "type": "request"
    "name": "request  name 1","media_urls": [
    "http://website.com/hiyup_dev/request/1611569983527_3192836205.mp4"],//or some other key name
    "type": "business"
    "media_urls": [
    "http://website.com/hiyup_dev/business/1611569983527_3192836205.mp4"],}

解决方法

db.businessreq.aggregate(
{
    $lookup: {
        from: 'businessreq',pipeline: [
            { $unwind: { path: "$products",preserveNullAndEmptyArrays: true } },{ $unwind: { path: "$products.media_urls",{ $match: { "products.media_urls": { $regex: ".mp4",$options: "$i" } } },{ $addFields: { "products.type": "product" } }
        ],as: 'breq'
    }
},{
    $lookup: {
        from: 'offer',pipeline: [
            { $unwind: { path: "$media_urls",{ $match: { "media_urls": { $regex: ".mp4",{ $addFields: { "type": "offer" } }
        ],as: 'off'
    }
},{
    $lookup: {
        from: 'productRequest',{ $addFields: { "type": "request" } }
        ],as: 'prodReq'
    }
},{
    $lookup: {
        from: 'businessreq',{ $addFields: { "type": "business" } }
        ],as: 'buiReq'
    }
},{
    "$project":
    {
        "Union": { $concatArrays: ["$breq.products","$off","$prodReq","$buiReq"] }
    }
},{ $unwind: "$Union" },{ $replaceRoot: { newRoot: "$Union" } },{
    "$project": {
        products: 0
    }
}

).pretty();