0

I have the following model (only the improtant type):

public class Model
{
    [BsonId]
    public ObjectId ModelId;
    public List<Game> Games;
}
public class Game
{
    [BsonId]
    public ObjectId GameId;
    public List<Match> Matches;
}
public class Match
{
    [BsonId]
    public ObjectId MatchId;
    public int[] Votes;
}

I have the following query:

var filter = Builders<Model>.Filter.And(
                            Builders<Model>.Filter.Eq(x => x.ModelId, modelId),
                            Builders<Model>.Filter.ElemMatch(x => x.Games, t => t.GameId == gameId));

var update = Builders<Model>.Update.Inc(x => x.Games[-1].Matches.First(t => t.MatchId == matchId).Votes[1], 2);
var options = new FindOneAndUpdateOptions<Model>()
{
    ReturnDocument = ReturnDocument.After
};

Im using FindOneAndUpdateAsync and Im able to retreive the data, but no changes are made. Tried couple of times, still no change.

Answers to Comments:

Is something being returned?

Yes, the entire document. moreover, when I use fixed indexes, e.g: games[0].matches[0] everything works fine.

Did I try to run command in shell?

No, unfortunartly, so far I haven't tried to work with the shell

Ori Refael
  • 2,888
  • 3
  • 37
  • 68
  • 2 follow ups: (1) does anything get returned when you just run a find with this filter? (2) have you tried running this in shell? If so, what is the syntax? – Craig Wilson Apr 18 '16 at 14:44
  • @CraigWilson answers in edit – Ori Refael Apr 18 '16 at 14:56
  • Oh, I think I see the issue... First(t => t.MatchId == matchId) isn't supported in this context. I don't think there is a way to do this in shell syntax. Arguably, we should be throwing an exception here. – Craig Wilson Apr 18 '16 at 19:55
  • Doesn't "x => x.Games[-1]" throw a System.ArgumentOutOfRangeException exception? – Onosa Apr 18 '16 at 20:34
  • Craig, so what one should do in case of lookup in nested enumrables? Onosa, no..-1 is the positional operator of mongo. It is replaces with the $ sign – Ori Refael Apr 18 '16 at 23:40

1 Answers1

0

I think you are supposed to use the update.push method for appending an array. https://docs.mongodb.org/manual/reference/operator/update/push/

Command command = CreateCommand(commandType,message,source); 
var filter = Builders<Sequence>.Filter.Eq("_id", seqid); 
var update = Builders<Sequence>.Update.Push("Commands", command); 
var result =  collection.UpdateOneAsync(filter, update);
Jacob
  • 371
  • 3
  • 12
  • Regardless to symantic mistake in your answer. This is not the problem in sub array. The problem was that, alegendly, there are 2 positinal operators when doing sub-sub array update – Ori Refael Apr 26 '16 at 16:48