1

I'm new to both C# and MongoDB. I'm trying to add a new object to a list in a MongoDB collection, here is my code:

ProjectModel:

class ProjectModel
{
    [BsonId]
    public Guid Id { get; set; }
    public string ProjectNumber { get; set; }
    public string ProjectName { get; set; }
    public PartModel[] Parts { get; set; }
}

PartModel:

class PartModel
{
    public string PartNumber { get; set; }
    public string PartName { get; set; }
}

Method Class

class MongoCRUD
{
    private IMongoDatabase db;
    
    public MongoCRUD(string database)
    {
        var client = new MongoClient();
        db = client.GetDatabase(database);
    }

    public void InsertPart<T>(string table,  Guid id, PartModel newPart)
    {       
        var collection = db.GetCollection<T>(table);
        var filter = Builders<T>.Filter.Eq("Id", id);          
        var update = Builders<ProjectModel>.Update.Push<PartModel>(e => e.Parts, newPart);
    }
}

Main Class:

class Program
{
    static void Main(string[] args)
    {
        MongoCRUD db = new MongoCRUD("PartsManagerDB");

        PartModel newPart = new PartModel()
        {
            PartName = "Lower Bracket",
            PartNumber = "4000"

        };

        db.InsertPart<PartModel>("Projects", new Guid("f3784ba4-c422-43e0-80fd-41bb87b20f10"), newPart);
    }
}

The project 93100 is already in my db, but after executing my code, no error comes up but the collection is not being updated as you can see below:

Snapshot from MongoDB Compass

3
  • 1
    I think the T should be a ProjectModel not PartModel when calling the method db.InsertPart Commented Jan 2, 2021 at 1:38
  • Maybe go through the update functionality one more time developer.mongodb.com/quickstart/csharp-crud-tutorial Commented Jan 2, 2021 at 5:30
  • @Sajid tried but didn't work Commented Jan 2, 2021 at 11:04

1 Answer 1

2

You are missing the line that actually executes the two things you already have, filter and update.

public void InsertPart<T>(string table,  Guid id, PartModel newPart)
{       
    var collection = db.GetCollection<T>(table);
    var filter = Builders<T>.Filter.Eq("Id", id);          
    var update = Builders<ProjectModel>.Update.Push<PartModel>(e => e.Parts, newPart);

    // This line will actually update your DB with the new value.
    collection.FindOneAndUpdate<T>(filter, update);
}

Documentation on FindOneAndUpdate() method.

Also, in your Main() method, you need to use the class, ProjectModel instead of PartModel when calling the InsertPart method.

db.InsertPart<**ProjectModel**>("Projects", new Guid("f3784ba4-c422-43e0-80fd-41bb87b20f10"), newPart);

In the InsertPart method, T is used in your filter's Builder and it should be ProjectModel to get the Project that you are updating... not Part. Part is being inserted to the ProjectModel's PartModel array so the T should be ProjectModel.

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

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.