You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用C#(.NET Core)向MongoDB插入数据时遇parallel arrays索引错误求助

Fixing the "cannot index parallel arrays" MongoDB Error in C# .NET Core

Hey there, let's break down why you're hitting this error and walk through how to fix it!

What's Causing This Exception?

MongoDB enforces a strict rule: you can't create multikey indexes on two separate array fields in the same collection (these are called "parallel arrays"). A multikey index is what MongoDB uses when you index a field that holds an array—like your Prop03 string array or Prop04 List<Test>.

This error pops up because:

  • You manually created indexes for both Prop03 and Prop04 in your MongoDB collection
  • Your C# code (or a framework/library you’re using) automatically added these indexes
  • There’s a compound index that includes both array fields

Step-by-Step Solutions

1. Check and Remove Conflicting Indexes

First, let's find the problematic indexes. Open the MongoDB Shell and run this command (replace yourCollectionName with your actual collection name):

db.yourCollectionName.getIndexes()

Look for entries where multikey: true and the index includes either Prop03 or Prop04. Once you identify them, delete one (or both, if you don’t need them) using:

// Delete index for Prop03
db.yourCollectionName.dropIndex("prop03_index_name")

// Or delete index for Prop04
db.yourCollectionName.dropIndex("prop04_index_name")

If there’s a compound index that includes both fields, delete that too.

2. Adjust Index Creation in Your C# Code

Check if your code explicitly creates indexes for both array fields. You might have something like this:

var indexKeys = Builders<YourModel>.IndexKeys;
var prop03Index = indexKeys.Ascending(m => m.Prop03);
var prop04Index = indexKeys.Ascending(m => m.Prop04);

// Remove one of these index creation calls
collection.Indexes.CreateOne(new CreateIndexModel<YourModel>(prop03Index));
collection.Indexes.CreateOne(new CreateIndexModel<YourModel>(prop04Index));

Remove the index creation for the field you don’t query frequently. If you need to query both, move to the next solution.

3. Refactor Your Data Model (If Business Logic Allows)

If you must query both array fields, restructure your model to avoid parallel arrays. For example, wrap one of the arrays in a nested object:

// New nested class
public class Prop03Container
{
    public string[] Items { get; set; }
}

// Update your main model
[BsonId] [BsonRepresentation(BsonType.ObjectId)] 
public string Id { get; set; }
[BsonElement("Prop01")] public int Prop01{ get; set; }
[BsonElement("Prop02")] public int Prop02{ get; set; }
[BsonElement("Prop03")] public Prop03Container Prop03{ get; set; } // Now an object, not an array
[BsonElement("Prop04")] public List<Test> Prop04{ get; set; }

MongoDB will create a multikey index on Prop03.Items instead of Prop03 directly, which won’t conflict with Prop04’s index.

Verify the Fix

After removing conflicting indexes or refactoring your model, re-run your insertion code. The MongoWriteException should no longer appear. If it does, double-check for any remaining indexes that include both array fields.

内容的提问来源于stack exchange,提问作者Salman Elahi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 00:12:29