将MongoDB的多个文档合并为单个文档

时间:2019-11-25 05:54:20

标签: mongodb mongodb-query

有人可以帮助我合并数组吗?

使用波纹管查询

db.products_new.aggregate([{"$match":{"$or":[{"name":{"$regex":"^Cosimo\\b"}},{"brand.brand_name":{"$regex":"Cosimo"}},{"categories":{"$elemMatch":{"$regex":"Cosimo"}}},{"model_number":{"$regex":"^Cosimo\\b"}},{"country_of_mfg":{"$regex":"^Cosimo\\b"}},{"keywords":{"$in":["Cosimo"]}},{"model_number":"Cosimo"},{"name":{"$regex":"\\bcosimo\\b"}}],"$and":[{"active":"1"}]}},{"$group":{"_id":{"brand_id":"$brand_id"},"a":{"$push":{"id":"$id","name":"$name","brand_id":"$brand_id","primary_image_url":"$primary_image_url","brand_name":"$brand.brand_name","slug":"$slug"}}}},{"$unwind":{"path":"$a","includeArrayIndex":"ranking"}},{"$project":{"_id":0,"brand_id":"$_id.brand_id","id":"$a.id","name":"$a.name","primary_image_url":"$a.primary_image_url","slug":"$a.slug","brand_name":"$a.brand_name","ranking":"$ranking"}},{"$sort":{"brand_id":1}},{"$facet":{"limit-2-per-brand_id":[{"$bucket":{"groupBy":"$ranking","boundaries":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99],"default":"Other","output":{"products_push":{"$push":{"_id":"$_id","group":1,"id":"$id","name":"$name","brand_id":"$brand_id","primary_image_url":"$primary_image_url","slug":"$slug","brand_name":"$brand_name"}}}}}]}},{"$unwind":"$limit-2-per-brand_id"},{"$project":{"products":{"$setUnion":[["$limit-2-per-brand_id.products_push"]]}}},{"$unwind":"$products"}])

当前输出

{ "products" : [ { "group" : 1, "id" : "28359", "name" : "ACE", "brand_id" : "1", "primary_image_url" : "https://dt4f7ywfipgvt.cloudfront.net/products_images/28359/optimized/ace-1.jpg", "slug" : "ace", "brand_name" : "Cosimo" } ] }
{ "products" : [ { "group" : 1, "id" : "28360", "name" : "ADA", "brand_id" : "1", "primary_image_url" : "https://dt4f7ywfipgvt.cloudfront.net/products_images/28360/optimized/ada-1.jpg", "slug" : "ada", "brand_name" : "Cosimo" } ] }
{ "products" : [ { "group" : 1, "id" : "28361", "name" : "BOWEN", "brand_id" : "1", "primary_image_url" : "https://dt4f7ywfipgvt.cloudfront.net/products_images/28361/optimized/bowen-1.jpg", "slug" : "bowen", "brand_name" : "Cosimo" } ] }
{ "products" : [ { "group" : 1, "id" : "28362", "name" : "BRUNO", "brand_id" : "1", "primary_image_url" : "https://dt4f7ywfipgvt.cloudfront.net/products_images/28362/optimized/bruno-1.jpg", "slug" : "bruno-Cosimo-sym", "brand_name" : "Cosimo" } ] }

预期产量

"products" : [
    { "group" : 1, "id" : "28359", "name" : "ACE", "brand_id" : "1", "primary_image_url" : "https://dt4f7ywfipgvt.cloudfront.net/products_images/28359/optimized/ace-1.jpg", "slug" : "ace", "brand_name" : "Cosimo" },
    { "group" : 1, "id" : "28360", "name" : "ADA", "brand_id" : "1", "primary_image_url" : "https://dt4f7ywfipgvt.cloudfront.net/products_images/28360/optimized/ada-1.jpg", "slug" : "ada", "brand_name" : "Cosimo" },
    { "group" : 1, "id" : "28361", "name" : "BOWEN", "brand_id" : "1", "primary_image_url" : "https://dt4f7ywfipgvt.cloudfront.net/products_images/28361/optimized/bowen-1.jpg", "slug" : "bowen", "brand_name" : "Cosimo" },
    { "group" : 1, "id" : "28362", "name" : "BRUNO", "brand_id" : "1", "primary_image_url" : "https://dt4f7ywfipgvt.cloudfront.net/products_images/28362/optimized/bruno-1.jpg", "slug" : "bruno-Cosimo-sym", "brand_name" : "Cosimo" }

1 个答案:

答案 0 :(得分:0)

使用下面的聚合选项并给我预期的输出可以解决此问题。

    'use strict';

    let selectSelector = document.getElementsByTagName('select'),
        optionsSelector = document.getElementsByTagName('option'),
        expensesItem = document.getElementsByClassName('expenses-item'),
        resultItem = document.getElementById('result-item'),
        expensesBtn = document.getElementsByTagName('button')[1]; 

    let sum;


    expensesBtn.addEventListener('click', function () {
        if (isNaN(expensesItem[0].value) || isNaN(expensesItem[1].value) || isNaN(expensesItem[2].value) || isNaN(expensesItem[3].value) || isNaN(expensesItem[4].value)) {

            resultItem.textContent = "Введите числовые данные ..."; 

            } else if (selectSelector.addEventListener('select', function () {
                optionsSelector[0].value == "mat";
                })) {

                sum = +expensesItem[0].value + +expensesItem[1].value + +expensesItem[2].value + +expensesItem[3].value + +expensesItem[4].value;
                resultItem.textContent = sum;

        }
    });
```````````````````````````````````````````````````````````````````````````````````````````````````