本文介绍了MongoDB C# 驱动程序投影 $elemMatch $的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将此查询从基于文本的过滤器转换为基于表达式的过滤器.查询部分没问题,但我在投影 "state.transfer.attempts.$": 1 时遇到问题.

I'm trying to translate this query from text-based filters to expression based.Query part is OK, but I'm having troubles with projection "state.transfer.attempts.$": 1.

项目 $ 的等价表达式是什么?

What is Expression equivalent for Project $ ?

提前致谢

db.Items.find({
    "state.transfer.attempts": {
    "$elemMatch": {
          "entityId": 1,
          "state" : "failed"
        }
  }
},
{
  "state.transfer.attempts.$": 1
})

推荐答案

根据 这篇博文,位置运算符是通过寻址-1-元素实现的:

According to this blog post, the positional operator is implemented by addressing the -1th-element:

Builders.Projection.Include(state => state.transfer.attempts[-1])

Builders.Projection.Include(state => state.transfer.attempts.ElementAt(-1));

this SO answer中提到了相同的解决方案.

The same solution is mentioned in this SO answer.

我试图在文档中找到这种行为的参考,但没有运气.

I've tried to find a reference of this behavior in the documentation, but without luck though.

这篇关于MongoDB C# 驱动程序投影 $elemMatch $的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 03:19