MongoDB C#Remove不起作用

use*_*339 2 collections mongodb mongodb-query mongodb-.net-driver

我有这个代码从mongofb整理中删除项目

private MongoCollection<T> GetCollection()
    {
       connectionString = "mongodb://localhost/?safe=true";
       server = MongoServer.Create(connectionString);
       database = server.GetDatabase("CSCatalog"); 

        return database.GetCollection<T>("myCollectionName");
    }
public bool  Delete(T entity)
    {            
        var id = typeof(T).GetProperty("Id").GetValue(entity,null).ToString();            
        var query = Query.EQ("_id",id);
        var finded = GetCollection().Find(query); // return null
        var result= GetCollection().Remove(query, MongoDB.Driver.RemoveFlags.Single);  // no errors, but don't remove 

        return esito.Ok; //return true but donn't remove.


    }
Run Code Online (Sandbox Code Playgroud)

GetCollection()方法检索正确的集合,我测试了它的宽度调试.在集合中有我想删除的项目,它具有我在第一行中重新获得的相同ID.

该实体有一些字段和一个名为"Id"的Objectid字段

pol*_*hnu 6

你创建的_id的类型是ObjectId类,你试图与字符串等同,所以它无法删除.使用

var queryId = new ObjectId(id);