我正在尝试使用单个集合来子查询数据。
等价的SQL查询:
Select * from customer where custid = (select custid from customer where device ID = 123 and date <= currentdate fetch first row only) where date<= current date发布于 2021-03-23 01:54:03
您可以在MongoDB中通过在聚合中使用$lookup来进行子查询。
如果您的查询是db.items.find({ tag: 'blue' }),则可以将其转换为如下所示的聚合
db.items.aggregate([
{ $match: { tag: 'blue' } },
{ $lookup: your_lookup_command }
])有关$lookup语法,请参阅https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/。如果您使用的是MongoDB 3.6或更高版本,则可以使用另一个嵌入式聚合来返回具有分组或高级过滤功能的更复杂的子查询。
https://stackoverflow.com/questions/66750450
复制相似问题