[英]MongoDb C# Driver - DeleteMany doesn't work with In Filter
I tried to delete dublicate records using MongoDb C# Driver.我尝试使用 MongoDb C# 驱动程序删除重复记录。 The script below neither raise an error nor delete any record.
下面的脚本既不会引发错误也不会删除任何记录。 Any advice ?
有什么建议吗?
[BsonIgnoreExtraElements]
public class ApiUsers
{
[BsonId]
[BsonRepresentation(BsonType.String)]
public ObjectId Id { get; set; }
public string UserEMail { get; set; }
}
var users = _mongoDbContext.ApiUsers.Find(f => f.UserEMail == email).ToList();
var oneUser = users[0];
var idsToDelete = users.Where(x => !x.Id.Equals(oneUser.Id)).Select(x => x.Id);
if (idsToDelete.Any())
{ //Delete Dublicates
var idsToDeleteFilter = Builders<ApiUsers>.Filter.In(t => t.Id, idsToDelete);
var result = _mongoDbContext.ApiUsers.DeleteMany(idsToDeleteFilter);
}
I tried also DeleteOne method like below, but it didn't work either,我也尝试过像下面这样的 DeleteOne 方法,但它也不起作用,
foreach (var idToDelete in idsToDelete)
_mongoDbContext.ApiChildUsers.DeleteOne(Builders<ApiChildUsers>.Filter.Eq(u => u.Id, idToDelete));
you could try the following approach where you get back an Id to keep and delete the rest like so:您可以尝试以下方法,获取一个 Id 以保留和删除其余部分,如下所示:
var filter = Builders<ApiUsers>.Filter.Where(u => u.UserEMail == "test@test.com");
var projection = Builders<ApiUsers>.Projection.Expression(u=>u.Id);
var options = new FindOptions<ApiUsers, string> { Projection = projection, Limit = 1 };
var idToKeep = collection.FindSync(filter, options).ToList()[0];
collection.DeleteMany(
u => u.UserEMail == "test@test.com" &&
u.Id != idToKeep);
as a sidenote, i'd like to suggest you store the IDs in the server as ObjectId
instead of string because it's less efficient as ObjectId only takes 12 bytes to store which would be less than the hexadecimal string representation of it.作为旁注,我建议您将 ID 存储在服务器中作为
ObjectId
而不是字符串,因为它的效率较低,因为 ObjectId 只需要 12 个字节来存储,这将小于它的十六进制字符串表示。
here's a less verbose test program using mongodb.entities:这是一个使用 mongodb.entities 的不那么冗长的测试程序:
using MongoDB.Entities; using System.Threading.Tasks; namespace TestApp { public class ApiUsers : Entity { public string UserEMail { get; set; } } internal static class Program { private static async Task Main() { await DB.InitAsync("test", "localhost"); await new[] { new ApiUsers { UserEMail = "test@test.com"}, new ApiUsers { UserEMail = "test@test.com"}, new ApiUsers { UserEMail = "test@test.com"}, new ApiUsers { UserEMail = "teX@teX.com"}, }.SaveAsync(); var idToKeep = (await DB.Find<ApiUsers, string>() .Match(u => u.UserEMail == "test@test.com") .Project(u => u.ID) .Limit(1) .ExecuteAsync())[0]; await DB.DeleteAsync<ApiUsers>( u => u.UserEMail == "test@test.com" && u.ID != idToKeep); } } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.