Update Embedded document in mongodb using C#

后端 未结 1 1619
小鲜肉
小鲜肉 2021-01-21 15:33

Suppose you have the next class. It contains the systems in which the agent has worked

public class AgentHistory
{
    public ObjectId Id { get; set; }
    publi         


        
相关标签:
1条回答
  • 2021-01-21 16:03

    You don't have to be so verbose: BsonValue.Create() and BsonArray.Create should not be required.

    In fact, the latter is the cause of your problem: BsonArray.Create creates arrays of value types. You need an array of objects, however. If you take a look at the available overloads of BsonArray.Create, I guess you'll be invoking BsonArray.Create(IEnumerable), which is not desirable.

    Have you tried to simply use

    MongoCollection.Update(query, Update.Set("Agents", updatedEntity.Agents), ...);
    

    instead?

    In JSON, the difference looks like this:

    Array of Values: [ val, val, ... ]

    Array of Objects: [ { ... }, { ... }, ... ]

    For example,

    Simple Array: [ "mongodb", "awesomness", ... ]

    Array of Objects: [ { userId: 2314234, comment: "Foo" }, { ... }, ... ]

    0 讨论(0)
提交回复
热议问题