Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 插入前检查MongoDB的唯一性_C#_Mongodb - Fatal编程技术网

C# 插入前检查MongoDB的唯一性

C# 插入前检查MongoDB的唯一性,c#,mongodb,C#,Mongodb,我有一个MongoDB集合,包含100万个唯一的数字 我有一个操作,我需要向集合中添加30000个数字,但是如果集合中已经有这些数字,我需要取消该操作 使用C,想法1是这样做: private void DoImport(List<long> numbersToImport) { // brings over 1 million records from the DB. Yikes! var numbersInDatabase = number

我有一个MongoDB集合,包含100万个唯一的数字

我有一个操作,我需要向集合中添加30000个数字,但是如果集合中已经有这些数字,我需要取消该操作

使用C,想法1是这样做:

private void DoImport(List<long> numbersToImport)
    {
        // brings over 1 million records from the DB. Yikes!
        var numbersInDatabase = numberRepository.GetAll();

        // convert list into a more lookup-able format
        var numbersHash = new HashSet<long>(numbersInDatabase);

        // determine if any numbers are already in the database
        var duplicates = numbersToImport.Where(number => numbersHash.Contains(number)); 
        if (duplicates.Any()) {
            // abort processing - at least one input list element is already in the database            
        }
    }
但是,仅仅为了做一个比较,就带来了500万个数字,存在着明显的性能和可扩展性问题

因为我有一个唯一的索引,所以想法2是批量插入,如果新插入的集合的计数与初始列表的计数不匹配,则删除所有新记录。我不喜欢这种不确定性。似乎在我进入数据库之前我应该了解更多关于唯一性的知识


在添加到集合之前,是否有一种可扩展且有效的方法来检查集合的唯一性?

此操作是一次性导入一次还是多次导入?每天一次或两次。这听起来像是整个大容量插入在出现重复时应该失败。我以为我有一个解决方案,但在进一步测试后,它的行为并不像我想象的那样。让我再做一些研究。