展开组中的两个嵌套数组mongodb nodejs

时间:2018-09-27 07:12:04

标签: node.js mongodb mongodb-query aggregation-framework

$unwindNodeJS查询中可以接受两个MongoDb方法吗?我正在尝试获取两个文档的所有不同子文档。

这是我的查询:

col.aggregate([
    {$match: {name: 'Facebook'}},

    {$unwind: "$products"},
    {$unwind: "$offices"},

    {$group:{
        "_id":  {
            CompanyName: "$name",
            HomepageURL: "$homepage_url",
            Description: "$description",
            NumberofEmployees: "$number_of_employees",
            TotalMoneyRaised: "$total_money_raised",
            FoundedYear: "$founded_year",
            CompanyCategory:{ $cond: { if: { $gte: [ "$number_of_employees", 10000 ] }, then: "BIG COMPANY", else: "SMALL COMPANY" }}    
                },
            Products: {$push: '$products.name'},             
            Offices: {$push: '$offices.description'}    
            }   
    },

    {$sort:{name: 1}}

]).toArray(function(err, docs) {
    console.log(docs);
    client.close();
});
});

我尝试展开两个嵌套数组,但是查询输出重复的值。 这是ProductsOffices的结果:

Products:
 [ 'Facebook Platform',
   'Facebook Platform',
   'Facebook Platform',
   'Facebook News Feed',
   'Facebook News Feed',
   'Facebook News Feed',
   'Facebook Chat',
   'Facebook Chat',
   'Facebook Chat',
   'Facebook Connect',
   'Facebook Connect',
   'Facebook Connect',
   'Facebook Mobile',
   'Facebook Mobile',
   'Facebook Mobile',
   'Facebook',
   'Facebook',
   'Facebook',
   'Facebook Lite',
   'Facebook Lite',
   'Facebook Lite',
   'Facebook Places',
   'Facebook Places',
   'Facebook Places' ],
Offices:
 [ 'Headquarters',
   'Europe HQ',
   'New York',
   'Headquarters',
   'Europe HQ',
   'New York',
   'Headquarters',
   'Europe HQ',
   'New York',
   'Headquarters',
   'Europe HQ',
   'New York',
   'Headquarters',
   'Europe HQ',
   'New York',
   'Headquarters',
   'Europe HQ',
   'New York',
   'Headquarters',
   'Europe HQ',
   'New York',
   'Headquarters',
   'Europe HQ',
   'New York' ] } ]

这是我想要的结果,不同的产品和不同的办公室:

[ { _id:
 { CompanyName: 'Facebook',
   HomepageURL: 'http://facebook.com',
   Description: 'Social network',
   NumberofEmployees: 5299,
   TotalMoneyRaised: '$2.43B',
   FoundedYear: 2004,
   CompanyCategory: 'SMALL COMPANY' },
Products:
 [ 'Facebook Platform',
   'Facebook News Feed',
   'Facebook Chat',
   'Facebook Connect',
   'Facebook Mobile',
   'Facebook',
   'Facebook Lite',
   'Facebook Places'  ],
Offices:
 [ 'Headquarters',
   'Europe HQ',
   'New York'] } ]

这是参考数据库:

 {
"_id" : ObjectId("52cdef7c4bab8bd675297d8e"),
"name" : "Facebook",
"homepage_url" : "http://facebook.com",
"description" : "Social network",
"number_of_employees" : 5299,
"total_money_raised" : "$2.43B",
"founded_year" : 2004,
"number_of_employees" : 5299,
"products" : [ 
    {
        "name" : "Facebook Platform",
        "permalink" : "facebook-platform"
    }, 
    {
        "name" : "Facebook News Feed",
        "permalink" : "facebook-news-feed"
    }, 
    {
        "name" : "Facebook Chat",
        "permalink" : "facebook-chat"
    }, 
    {
        "name" : "Facebook Connect",
        "permalink" : "facebook-connect"
    }, 
    {
        "name" : "Facebook Mobile",
        "permalink" : "facebook-iphone-app"
    }, 
    {
        "name" : "Facebook",
        "permalink" : "facebook-zero"
    }, 
    {
        "name" : "Facebook Lite",
        "permalink" : "facebook-lite"
    }, 
    {
        "name" : "Facebook Places",
        "permalink" : "facebook-places"
    }
],
"offices" : [ 
    {
        "description" : "Headquarters",
        "address1" : "1601 Willow Road",
        "address2" : "",
        "zip_code" : "94025",
        "city" : "Menlo Park",
        "state_code" : "CA",
        "country_code" : "USA",
        "latitude" : 37.41605,
        "longitude" : -122.151801
    }, 
    {
        "description" : "Europe HQ",
        "address1" : "",
        "address2" : "",
        "zip_code" : "",
        "city" : "Dublin",
        "state_code" : null,
        "country_code" : "IRL",
        "latitude" : 53.344104,
        "longitude" : -6.267494
    }, 
    {
        "description" : "New York",
        "address1" : "340 Madison Ave",
        "address2" : "",
        "zip_code" : "10017",
        "city" : "New York",
        "state_code" : "NY",
        "country_code" : "USA",
        "latitude" : 40.7557162,
        "longitude" : -73.9792469
    }
],

附加查询(不展开):

    col.aggregate([
    {$match: {name: 'Facebook'}},

    //{$unwind: "$products"},
    //{$unwind: "$offices"},

    {$group:{
        "_id":  {
            CompanyName: "$name",
            HomepageURL: "$homepage_url",
            Description: "$description",
            NumberofEmployees: "$number_of_employees",
            TotalMoneyRaised: "$total_money_raised",
            FoundedYear: "$founded_year",
            CompanyCategory:{ $cond: { if: { $gte: [ "$number_of_employees", 10000 ] }, then: "BIG COMPANY", else: "SMALL COMPANY" }}    
            },
            Products: {$addtoset: '$products.name'},             
            //Offices: {$push: '$offices.description'}    
            }
    },

    {$sort:{name: 1}}

]).toArray(function(err, docs) {
    console.log(docs);
    client.close();
});

在不使用unwind o的情况下也是如此,使用addtoset时结果为NULL,使用unwind的push正常工作(仅$ unwind 1)

1 个答案:

答案 0 :(得分:0)

在小组赛阶段只需将您的2个$ push替换为$addToSet:它将只“推送”一次值,而不会造成重复