1

I have a little bit problem with an data update in mongoDB. I have an array embedded in an other array who is embedded in a document (yaaaaay ^^). So my collection is like that :

{
    "_id" : ObjectId("5965e0b4f1042c4c3f1d8edc"),
    "Builder" : "Google",
    "Family" : "Car",
    "ProductName" : "ABCD_80A_PU170017",
    "ProductNumber" : "N 1",
    "Test" : [ 
        {
            "Num_Test" : "PU170017",
            "Date_Start" : ISODate("2017-07-12T00:00:00.000Z"),
            "Bench": [ 
                {
                    "Room": 1,
                    "Num" : 1,
                    "Designation" : "AC1; AC3",
                    "IP" : "123.456.78.9",
                    "Capacity" : 12,
                }
            ],
            "User" : [ 
                {
                    "Number" : "EBBW396",
                    "Password" : "titi",
                    "Name" : "NDJO",
                    "Surname" : "Charles",
                    "Tel" : "06XXXXXX",
                    "isAdmin" : true
                }
            ]
        }
    ]
}

And i want to insert a new array embedded in "Test".

So i found my happiness here : https://docs.mongodb.com/getting-started/csharp/update/ AND How to use C# to insert document into existing embedded documents?

And i have write this code :

        static void Main(string[] args)
    {
        IMongoClient client = new MongoClient("mongodb://10.194.157.199:27017");
        IMongoDatabase database = client.GetDatabase("PINEA");
        IMongoCollection<Product> collection = database.GetCollection<Product>("Product");

        var filter = Builders<Product>.Filter.Eq("Test.Num_Test", "PU170017");
        var meter = new Meter();
        meter.Value= 12;
        meter.Date = DateTime.Now;
        var meterList = new List<Meter>();
        meterList.Add(meter);

        var update = Builders<Product>.Update.Push("Result", new Result
        {
            MeterList = meterList
        });

        var result = collection.UpdateOne(filter, update);
    }

But nothing happens, nothing has added or modified. I miss something important but i don't understand what is it.

Finally, here are my classes:

class Result
{
    public List<Meter> MeterList { get; set; }
    public List<Problem> Problem { get; set; }
}
class Meter
{
    public int? Value { get; set; }
    public DateTime? Date { get; set; }
}
class Test
{
    public string Num_Test { get; set; }
    public DateTime? Date_Start { get; set; }
    public List<Bench> Bench { get; set; }
    public List<User> User { get; set; }
    public List<Result> Result { get; set; }
}
class Product
{
    public string Builder { get; set; }
    public string Fammily { get; set; }
    public string ProductName { get; set; }
    public string ProductNumber { get; set; }
    public List<Test> Test { get; set; }
}

To explain the context, I need to update the array "result" regularly and maintain data arborescence like that. After the end of the test, data aren't modify anymore.

PS : I'm still a student but I work part time

Thanks

2
  • I can't edit i don't know why. I forgot to said hello ^^' so Hello everyone Commented Jul 20, 2017 at 14:51
  • I found my mistake... I don't work in Async, just change UpdateOneAsync to UpdateOne Commented Jul 25, 2017 at 7:38

0

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.