C# 使用非通用EQ查询从集合中删除文档

C# 使用非通用EQ查询从集合中删除文档,c#,.net,mongodb,mongodb-.net-driver,C#,.net,Mongodb,Mongodb .net Driver,我试图使用非通用EQ查询从我的集合中删除一个文档,但它不会删除任何内容。使用通用EQ查询,文档将成功删除 这是我存储在MongoDB中的对象 public class UserDto { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string UserName { get; set;

我试图使用非通用EQ查询从我的集合中删除一个文档,但它不会删除任何内容。使用通用EQ查询,文档将成功删除

这是我存储在MongoDB中的对象

public class UserDto {
    public int Id { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string UserName { get; set; }
}
下面是我如何从集合中删除文档的示例代码

var collection = database.GetCollection<UserDto>(typeof(UserDto).Name);

var single = collection.AsQueryable<UserDto>().FirstOrDefault(p => p.Id == 46);

// using the generic version will remove the document.
//var result = collection.Remove(Query<UserDto>.EQ(p => p.Id, 46));

// using the non-generic version will not remove the document.
var result = collection.Remove(Query.EQ("Id", BsonValue.Create(46)));
var collection=database.GetCollection(typeof(UserDto).Name);
var single=collection.AsQueryable().FirstOrDefault(p=>p.Id==46);
//使用通用版本将删除该文档。
//var result=collection.Remove(Query.EQ(p=>p.Id,46));
//使用非通用版本不会删除文档。
var result=collection.Remove(Query.EQ(“Id”,BsonValue.Create(46));
my MongoQuery删除文档的设置是否有问题


我使用的是MongoDB 2.6.1和MongoDB驱动程序C#1.9.1.221

,如果您没有进行其他配置,则驱动程序会将您的
Id
字段视为文档的Id。这意味着
MongoDB
中的字段将是“\u id”,而不是“id”

当您使用通用查询时,驱动程序会为您进行翻译。非泛型查询应如下所示:

var result = collection.Remove(Query.EQ("_id", 46));