Can not delete document in db

I can not delete document in db it deletes all data inside but empty document is still in db. How can not delete hole document ?

Please share how you did that.

var client = new MongoClient(connectionString);
var database = client.GetDatabase(“SoftMasterDb”);

var collection = database.GetCollection(collectionName);

try
{
// Convert the string ID to ObjectId
var objectId = ObjectId.Parse(id);
//
var filter1 = Builders.Filter.Eq(item => item.Id, id);
await collection.DeleteManyAsync(filter1);
//await collection.DeleteOneAsync(filter1);

}
catch (Exception ex)
{
Console.WriteLine($“An error occurred: {ex.Message}”);
}

Many things.

It could be that if you have an

it does not have a field named Id or id. I do not know the .net driver so I am not too sure. In particular, what is the value of item.Id?

If item.Id equals _id, then you probably have a type mismatch. It looks like you tried to avoid that with

but you ignore this type conversion by querying with id in

It could potentially work if you use the variable objectId rather than id.

1 Like

@nikonarimanidze23, it has been more that a week since I took the time to read, think and reply to your post.

Please provide followup.

If my proposition worked, please mark my post as the solution. If it fails provide details about what did not work. If you found another solution please share.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.