0

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?

MLR
  • 23
  • 5

1 Answers1

2

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} }}
] 
amy
  • 36
  • 2
  • Thank you amy for the response. I tried the query and it returned 14 records. But honestly, I'm not sure what else to do with them. Here's an example : `{ "_id" : { "id" : { "$oid" : "5a2abc0409dc752d9ea5acb2" }, "field" : "claimDate" }, "count" : 2.0, "vals" : [ { "$date" : "2017-12-11T18:32:00.000+0000" }, { "$date" : "2017-12-11T18:32:00.000+0000" } ] }` Additionally, when I enter that date in the search, I only get one record. What am I missing? `db.getCollection("shipments").find({claimDate: ISODate('2017-12-11T18:32:00.000Z')})` – MLR Jun 23 '23 at 14:15
  • By using the provided query, I was able to identify the problematic entries. I made a copy of them and then removed them from the DB. I was then able to create the dynamic index. After that, I recreated the deleted entries, and everything went smoothly without any errors, and the index remained functional. I'm not sure of the exact reason why it wasn't working before, but for now, my problem is resolved. Thank you! – MLR Jun 28 '23 at 18:15