6

I am using the latest MongoDB C# Driver and I am having a problem with one of my queries. I am trying to update a field in a document in Mongo and identify this document by it's id field. I have written similar queries that filter the documents based on other field but for some wreason it doesn't work with the id field.

My Query:

public static async Task<bool> Delete(string Guid)
{
var database = new MongoDB();
var thingsCollection = database.Things;
var builder = Builders<ThingsModel>.Filter;
var filter = builder.Eq("_id", Guid);
var update = Builders<ThingsModel>.Update
    .Set("IsActive", false);
var updateResult = await thingsCollection.UpdateOneAsync(filter, update);
return true;         
}

I realise I am returning true each time but that is just there for testing. The updateResult object that comes back from Mongo says it didn't find any matches despite the being a thing in the thingsCollection with the Guid passed in the as a parameter. I have also marked the Guid property to be the ID Field in the collection as follows:

[BsonId]
public Guid Guid { get; set; }

Also I know I need to change the name from Guid to something else as it bad practice but one thing at a time :) I appreciate any help!

1 Answer 1

5

there could be an issue with your naming.

Using linqu syntax we can have this (_id is used as id field ):

var update = Builders<ThingsModel>.Update.Set(a => a.IsActive, false); 
var result = _collection.UpdateOne(model => model._id == _id, update);

full code here

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.