【问题标题】:MongoDB :: Convert SQL to MongoDBMongoDB :: 将 SQL 转换为 MongoDB
【发布时间】:2021-03-09 09:18:29
【问题描述】:

任何人都可以将 SQL 查询转换为 mongo 查询吗::

select customerId
from CustomerInfo
where customerId not in (select customerId
from CustomerInfo
where channel = 'A');

在这里,我想查找不存在频道“A”的 customerId。

【问题讨论】:

  • 为什么不select customerId FROM CustomerInfo WHERE channel <> 'A'
  • 它也适用于 SQL。但我想要 mongo 查询。
  • 应该是db.CustomerInfo.find({channel: {$ne: "A"}}, {customerId: 1})

标签: sql mongodb mongodb-query aggregation-framework


【解决方案1】:
var customerIds = db.CustomerInfo.find({channel:'A'}).map(function(like) {  
  return like. customerId;  
}); 


db.CustomerInfo.distinct("customerId",{customerId:{$customerIds}})

【讨论】:

  • 介意解释一下您的解决方案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-28
  • 2022-01-03
  • 2013-11-06
相关资源
最近更新 更多