我正在尝试使用非通用EQ-Query从我的集合中删除文档,但它不会删除任何内容。使用通用EQ-Query,文档将被成功删除。
这是我在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
答案 0 :(得分:1)
如果您尚未配置,则驱动程序会将Id
字段视为文档的ID。这意味着MongoDB
中的字段将为“_id”而不是“Id”。
当您使用通用查询时,驱动程序会为您进行翻译。非泛型查询应如下所示:
var result = collection.Remove(Query.EQ("_id", 46));