在 `$elemMatch` 中引用子文档的另一个字段
我正在尝试在$ match
聚合阶段中执行$ elemmatch
,我想在数组中查找是否有文档(commitchments
)属性trackSthisweek
的子图表小于其频率
属性,但我不确定如何引用所讨论的子记录的另一个字段,我提出了:
{
$match: {
commitments: {
$elemMatch: {
tracksThisWeek: {
$lt: '$frequency',
},
},
},
},
},
我在集合中有一个文档,应从此聚合中返回,但不是,任何帮助都将受到赞赏:)
I'm trying to perform an $elemMatch
in a $match
aggregation stage where I want to find if there is a document in an array (commitments
) of subdocuments whose property tracksThisWeek
is smaller than its frequency
property, but I'm not sure how can I reference another field of the subdocument in question, I came up with:
{
$match: {
commitments: {
$elemMatch: {
tracksThisWeek: {
$lt: '$frequency',
},
},
},
},
},
I have a document in the collection that should be returned from this aggregation but isn't, any help is appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是无法做到的,您无法在查询语言中引用任何字段,您可以使用
$ expr
与聚合运算符,诸如此类:mongo playground
This can't be done, you can't reference any fields in the query language, What you can do is use
$expr
with aggregation operators, like this:Mongo Playground