shatl
shatl

Reputation: 910

MongoDB C# Driver projection $elemMatch $

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 $ ?

Thanks in advance

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

Upvotes: 1

Views: 1101

Answers (1)

Vegar
Vegar

Reputation: 12898

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

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

or

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

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.

Upvotes: 2

Related Questions