简体   繁体   中英

Add item in nested array (mongodb and C#)

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; }
}

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM