簡體   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