我们如何将这个SQL查询转换为MongoDB查询?

时间:2022-10-17 15:51:11

Let's suppose I have a collection which tracks the number of visits of a product for every customer email id. How can I convert the following query into MongoDB query.

假设我有一个跟踪每个客户电子邮件ID的产品访问次数的集合。如何将以下查询转换为MongoDB查询。

select product_id, count(customer_email) 
       from table_sample 
       group by product_id.

I'm completely a novice at MongoDB. Any help is appreciated.

我完全是MongoDB的新手。任何帮助表示赞赏。

1 个解决方案

#1


0  

Something like:

db.table_sample.aggregate( [
   {
     $group: {
        _id: "$product_id",
        count: { $sum: 1 }
     }
   }
] )

https://docs.mongodb.org/v3.0/reference/sql-aggregation-comparison/

#1


0  

Something like:

db.table_sample.aggregate( [
   {
     $group: {
        _id: "$product_id",
        count: { $sum: 1 }
     }
   }
] )

https://docs.mongodb.org/v3.0/reference/sql-aggregation-comparison/