0

I have the following document called Attendances

{
    "_id" : ObjectId("5a4ffb00762caf6b54f61ebb"),
    "AttnDate" : ISODate("2018-01-05T22:24:00.490Z"),
    "AllAttendances" : [ 
        {
            "FullName" : "DOMAIN\Zack",
            "Logged" : ISODate("2018-01-05T22:23:46.835Z"),
            "Pauses" : [
                {
                    PauseStartAt: ISODate("2018-01-05T22:30:46.835Z"),
                    PauseEndAt: ISODate("2018-01-05T22:35:46.835Z")
                }
            ]
        }
    ]
}

How can i add new items to Pauses. This is my attempt but i have this error "Cannot convert lambda expression to type 'fielddefinition because it is not a delegate type.

My attempt

var filter = Builders<Attendance>.Filter.Eq(a => a.Id, currentAttn.Id) & Builders<Attendance>.Filter.ElemMatch(s => s.AllAttendances, Builders<TimeRecord>.Filter.Eq(n => n.FullName, userName));
var update = Builders<Attendance>.Update.Push(e => e.AllAttendances[-1].Pauses, pauses);
context.Attendances.FindOneAndUpdate(filter, update);

I followed this guide

Attendance Class

public class Attendance
{
    [JsonConverter(typeof(ObjectIdConverter))]
    public ObjectId Id { get; set; }
    [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
    public DateTime AttnDate { get; set; }
    public List<TimeRecord> AllAttendances { get; set; }
}

TimeRecord Class (AllAttendances)

public class TimeRecord
{
    public string FullName { get; set; }
    [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
    public DateTime Logged { get; set; }
    public List<Pause> Pauses { get; set; }
}

Pause Class

public class Pause
{
    [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
    public DateTime PauseStartedAt { get; set; }
    [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
    public DateTime PauseEndedAt { get; set; }
}

2 Answers 2

0

You need to update your filter to

var filter = Builders<Attendance>.Filter.Eq(a => a.Id, id) &
             Builders<Attendance>.Filter.ElemMatch(s => s.AllAttendances, x => x.FullName == userName);

The first argument of ElemMatch is the field, the second argument is the filter.

Sign up to request clarification or add additional context in comments.

Comments

0

Looking at it from a different angle, I would suggest you don't use ObjectIDs in c#. I always define ObjectIds as strings in my models and use the Bson attribute decorators to define them as ObjectId's in the database

[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }

Purely for the pain it causes trying to use ObjectIds in C#. Strings are much easier to handle. Your document in mongodb will still look the same, and you will not need to cast as object id's in your code at all:

_id : ObjectId("xxxxxxx")

This should help you get around the issue of the compiler not knowing how to do the conversion

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.