具有多个条件的 Mongo 3.6 聚合查找

编程入门 行业动态 更新时间:2024-10-25 06:30:29
本文介绍了具有多个条件的 Mongo 3.6 聚合查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我有一个只有一个集合 data 的 Mongo DB.在这个集合中,我有以下文档:

Suppose I have a Mongo DB with only one collection data. In this collection, I have the following documents:

{ "type": "person", "value": { "id": 1, "name": "Person 1", "age": 10 } }, { "type": "person", "value": { "id": 2, "name": "Person 2", "age": 20 } }, { "type": "prescription", "value": { "drug": "Bromhexine", "patient": 2 } }, { "type": "prescription", "value": { "drug": "Aspirin", "patient": 1 } }

有了这些记录,我想在 "type": person 和 "type":处方 上的 value.id 文档之间进行 JOIN= value.patient.

With those records, I'd like to make a JOIN between documents with "type": person and "type": prescription on value.id = value.patient.

我已经尝试了以下阶段的聚合:

I've already tried an aggregation with the following stages:

{ "$match": { "type": "person" } }, { "$lookup": { "from": "data", "let": { "patient": "$value.id"}, "pipeline": [ { "$match": { "$expr": { "type": "prescription", "value.patient": "$$patient" } } } ], "as": "prescription" } }

但它会产生错误FieldPath 字段名称可能不包含'.'.我认为这是由于 "let": { "patient": "$value.id"}, 行.相反,如果我尝试使用双美元符号 ($$)(如 here 所示)),结果是错误Use of undefined variable: value.

But it produces an error FieldPath field names may not contain '.'. Which I believe is due to the "let": { "patient": "$value.id"}, line. If instead I try double dollar signs ($$) (as seen here), the result is the error Use of undefined variable: value.

知道如何进行这种聚合吗?

Any idea of how I can make this aggregation?

推荐答案

在 $lookup 阶段完成以下管道对象

Done with the following pipeline object inside the $lookup stage

"pipeline": [ { "$match": { "$expr": { "$and": [ { "$eq": [ "$type", "prescription" ] }, { "$eq": [ "$value.patient", "$$patient" ] } ] } } } ]

更多推荐

具有多个条件的 Mongo 3.6 聚合查找

本文发布于:2023-11-22 20:53:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618983.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   条件   Mongo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!