简体   繁体   English

MongoDb C# 驱动程序 - DeleteMany 不适用于 In Filter

[英]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.

相关问题 Find() 在 mongoDB C# 驱动程序与 uuid 不工作 - Find() in mongoDB C# driver with uuids doesn't work MongoDB不会在集合中插入第二个文档(C#驱动程序) - MongoDB doesn't insert a second document into a collection (C# Driver) mongodb 驱动程序 C# - 过滤器表达式中的反射 - mongodb driver c# - reflection in filter expression MongoDB C# 2.14驱动滤波器。 - MongoDB C# 2.14 Driver Filter.In 使用正则表达式 MongoDB C# 驱动程序过滤 - Filter with regex MongoDB C# driver MongoDb Driver 2.0 C#Filter和Aggregate - MongoDb Driver 2.0 C# Filter and Aggregate C# MongoDB 驱动程序:在 MongoDB 中找不到对 AnyIn 过滤器运行复杂查询的方法 - C# MongoDB Driver: Can't find the way to run complex query for AnyIn filter in MongoDB 包含过滤器搜索不适用于devexpress gridview c# - Contains filter search doesn't work for devexpress gridview c# 为什么 MongoDB C# 驱动程序默认不使用 BsonType.Decimal128 表示小数? - Why doesn't MongoDB C# driver use BsonType.Decimal128 representation for decimals by default? 使用 C# 驱动程序(MongoDB)更新完整文档(如果“Id”存在则替换所有文档//如果“Id”不存在则插入) - Upsert a complete document (replace all if "Id" exists // insert if "Id" doesn't exists ) with C# Driver (MongoDB)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM