繁体   English   中英

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

[英]how to merge multiple collections in mongodb node js using mongodb driver

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

商业收藏

{
    
    _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",
        "lower_price": "0.00",
        "media_urls": ["http://website.com/hiyup_dev/product/1611569983527_3192836205.jpeg"],
        "higher_price": "0.00",
        "description": "Biscuits-1",
    }],
    "status": 1,
    "country_code": ""
}

优惠集合

{ 
    "_id": 1,
    "name": "offer name 1",
"user_id": 1,
    "lower_price": "0.00",
    "media_urls": ["http://website.com/hiyup_dev/offer/1611569983527_3192836205.jpeg",
    "http://website.com/hiyup_dev/offer/1611569983527_3192836205.mp4"],
    "higher_price": "0.00",
    "description": "Biscuits",
}, {
    "_id": 2,
    "name": "offr name2", "user_id": 1,
    "lower_price": "0.00",
    "media_urls": ["http://website.com/hiyup_dev/offer/1611569983527_3192836205.jpeg"],
    "higher_price": "0.00",
    "description": "Biscuits-1",
}

产品请求集合

   { 
    "_id": 1,
    "name": "request  name 1", "user_id": 1,
    "lower_price": "0.00",
    "media_urls": ["http://website.com/hiyup_dev/request/1611569983527_3192836205.jpeg",
    "http://website.com/hiyup_dev/request/1611569983527_3192836205.mp4"],
    "higher_price": "0.00",
    "description": "Biscuits",
}, {
    "_id": 2,
    "name": "request name2", "user_id": 1,
    "lower_price": "0.00",
    "media_urls": ["http://website.com/hiyup_dev/product/1611569983527_3192836205.jpeg"],
    "higher_price": "0.00",
    "description": "Biscuits-1",
}

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

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

但我无法组合多个collections。 当我使用放松。 重复数据来了。

我的预期输出是

{
    "_id": 2, //or some other key name like product_id
    **"type": "products"**
    "name": "Dog Biscuits-1",
    "lower_price": "0.00",
    "media_urls": [
    "http://website.com/hiyup_dev/product/1611569983527_3192836205.mp4"],
    "higher_price": "0.00",
     "description": "Biscuits-1",
},
{
    "_id": 1,//or some other key name
    "type": "offer"
    "name": "offer name 1",
    "lower_price": "0.00",
    "media_urls": [
    "http://website.com/hiyup_dev/offer/1611569983527_3192836205.mp4"],
    "higher_price": "0.00",
    "description": "Biscuits",
},
{
    "_id": 1,//or some other key name
    "type": "request"
    "name": "request  name 1",
    "lower_price": "0.00",
    "media_urls": [
    "http://website.com/hiyup_dev/request/1611569983527_3192836205.mp4"],
    "higher_price": "0.00",
    "description": "Biscuits",
},
{
    "_id": 1,//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", preserveNullAndEmptyArrays: true } },
            { $match: { "products.media_urls": { $regex: ".mp4", $options: "$i" } } },
            { $addFields: { "products.type": "product" } }
        ],
        as: 'breq'
    }
},
{
    $lookup: {
        from: 'offer', pipeline: [
            { $unwind: { path: "$media_urls", preserveNullAndEmptyArrays: true } },
            { $match: { "media_urls": { $regex: ".mp4", $options: "$i" } } },
            { $addFields: { "type": "offer" } }
        ],
        as: 'off'
    }
},
{
    $lookup: {
        from: 'productRequest', pipeline: [
            { $unwind: { path: "$media_urls", preserveNullAndEmptyArrays: true } },
            { $match: { "media_urls": { $regex: ".mp4", $options: "$i" } } },
            { $addFields: { "type": "request" } }
        ],
        as: 'prodReq'
    }
},
{
    $lookup: {
        from: 'businessreq', pipeline: [
            { $unwind: { path: "$media_urls", preserveNullAndEmptyArrays: true } },
            { $match: { "media_urls": { $regex: ".mp4", $options: "$i" } } },
            { $addFields: { "type": "business" } }
        ],
        as: 'buiReq'
    }
},
{
    "$project":
    {
        "Union": { $concatArrays: ["$breq.products", "$off", "$prodReq", "$buiReq"] }
    }
},
{ $unwind: "$Union" },
{ $replaceRoot: { newRoot: "$Union" } },
{
    "$project": {
        products: 0
    }
}

)。漂亮的();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM