我试图使用非通用的情商查询从我的集合中删除一个文档,但它没有删除任何内容。使用通用的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)));我的MongoQuery删除文档的设置有问题吗?
我使用的是MongoDB 2.6.1和MongoDB驱动程序C# 1.9.1.221
发布于 2014-05-19 09:14:30
如果您没有进行配置,否则您的Id字段将由驱动程序考虑文档的Id。这意味着MongoDB中的字段将是"_id“而不是"Id”。
使用泛型查询时,驱动程序将为您进行转换。非泛型查询应该如下所示:
var result = collection.Remove(Query.EQ("_id", 46));https://stackoverflow.com/questions/23732014
复制相似问题