doufeng2877 2016-10-03 19:13
浏览 24
已采纳

如果嵌入存在就放松

I have highly nested mongodb set of objects, so my collection is:

{
   "_id":17846384es,
   "company_name":"company1",
   "company_location":"city1",
    "phone":"xxxxx",
   "open_time":"xxx",
   "products":[
       {
         "product_id":"123785",
         "product_name":"product1",
         "user_votes":[
              {
                 "user_id":1,
                  "vote":1
              },
              {
                 "user_id":2,
                  "vote":2
              }
          ]
       },
       {
         "product_id":"98765",
         "product_name":"product2",
         "user_votes":[
              {
                 "user_id":5,
                  "vote":3
              },
              {
                 "user_id":3,
                  "vote":3
              }
          ]
       }
    ]
}  

I used some operation like sort and sum on this sub of documents so i used

db.products.aggregate([
    { $unwind: "$products" },
    { $project: {
        company_name: 1,
        products: {
            product_id: 1,
            product_name: 1,
            user_votes: 1,
            votes: { $sum: "$products.user_votes.vote" }
        } 
    } },
    { $sort: { totalVotes: -1 } },
    {
        $group: {
            _id: "$_id",
            company_name: { $first: "$company_name" },
            products: { $push: "$products" }
        }
    }
])  

to get this result :

{
   "_id":17846384es,
   "company_name":"company1",
   "products":[
       {
         "product_id":"98765",
         "product_name":"product2",
         "user_votes":[
              {
                 "user_id":5,
                  "vote":3
              },
              {
                 "user_id":3,
                  "vote":3
              }
          ]
        "votes":6
       },
       {
         "product_id":"123785",
         "product_name":"product1",
         "user_votes":[
              {
                 "user_id":1,
                  "vote":1
              },
              {
                 "user_id":2,
                  "vote":2
              }
          ],
          "votes":3
       }
    ]
}  

but if product document dosen't exist I have an empty result. I want to get a result with company information even if product document doesn't exist.

  • 写回答

1条回答 默认 最新

  • dongwanqiang_2017 2016-10-03 20:59
    关注

    Firstly, you can use "preserveNullAndEmptyArrays" to preserve the company which doesn't have product array.

    The below query should output the company names even if it doesn't have products array.

    I think you are trying to sum the votes and sort the product array to get the highest number of products as first element in the array. The below query should produce the result.

    db.product.aggregate([
        { $unwind: { path : "$products", preserveNullAndEmptyArrays : true }},
        { $project: {
            company_name: 1,
            products : { $ifNull : ["$products", "0"]}}            
        },
        { $project: {
            company_name: 1,
            products: {
                product_id: 1,
                product_name: 1,
                user_votes: 1,
                votes: { $sum: "$products.user_votes.vote" }
            } 
        } },
        { $sort: { "products.votes": -1 } },
        {
            $group: {
                _id: "$_id",
                company_name: { $first: "$company_name" },
                products: { $push: "$products" }
            }
        },  
    
    ]);  
    

    In case if you want to keep the companies which doesn't have product at the end. You can add this additional sort as the last pipeline in the above query.

    { $sort: { "products.votes": -1 } },
    

    Output of companies which doesn't have product array would like as below:-

    {
        "_id" : ObjectId("57f2bd50dd4752c20947fd03"),
        "company_name" : "company2",
        "products" : [ 
            {
                "votes" : 0
            }
        ]
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP