mongodbindexingmongodb-atlas-search

Error "DocValuesField appears more than once" encountered while creating a dynamic index for MongoDB Atlas Search


In MongoDB Altlas, I try to use dynamic Index, but I have this error in the index creation status : "Your index could not be built: Unexpected error: DocValuesField "$type:date/claimDate" appears more than once in this document (only one value is allowed per field)"

I try to look at the possibles values for the field claimDate for all the collection.

db.shipments.aggregate([
    { $group: { 
        _id: { $type: "$claimDate" }, 
        count: { $sum: 1 },
        sample: { $first: "$$ROOT" }
    } }
])

Possibles values are : null, date, missing

Atlas Search could handle this 3 possibles types.

Can you help me figure out what is preventing the creation of this dynamic index?


Solution

  • You likely have multiple documents with the same _id in your collection. You can run this query to find the duplicates:

    [
      {$project:{
         fields:{$objectToArray:'$$ROOT'}
      }},
      {$unwind: {
        path: '$fields'
      }}, 
      {$group: {
        _id: {id: '$_id', field:'$fields.k'},
        count:{$sum:1},
        vals:{$push:'$fields.v'}
      }}, 
      { $match: { count:{$gt:1} }}
    ]