1

I have a mongoDB for a list of issues. This record has a children attribute which is an empty list at first. The next time an issue with the same title occurs I want to append to the entry that's already being created.

I'm trying the following which is sorta working. But is giving an error when I try to use the $addToSet.

The error I get: Updating the path 'children' would create a conflict at 'children'

This is what i'm trying to do at the moment.

db.issues.update_one({'title': issue['title']},
                             {
                                 "$setOnInsert": {"insertion_date": now, "children": []},
                                 "$set": {"last_update_date": now},
                                 "$addToSet": {"children": {'title': issue['title'], 'date': now} },
                             },
                             upsert=True)

The first time an entry is created it should look like this

{
    "_id" : ObjectId("5b588b305b2eec7d7029fb4a"),
    "title" : "title_1",
    "last_update_date": ISODate("2018-07-25T14:41:50.168Z"),
    "children" : [],
}

Next time an entry with the same title appears it should just get appended to the children key of that record. The $addToSet should be unique because of the timestamp so it wouldn't be the same entry being added to that children list

{
    "_id" : ObjectId("5b588b305b2eec7d7029fb4a"),
    "title" : "title_1",
    "children" : [ 
                    {
                        "title": title_1,
                        "date": ISODate("2018-07-25T14:41:50.168Z")
                    } 
                 ],
}

Update

If I do this, it works but the first entry will also add it to the children key as opposed to it being an empty list.

db.issues.update_one({'title': issue['title']},
                             {
                                 "$setOnInsert": {"insertion_date": now},
                                 "$set": {"last_update_date": now},
                                 "$addToSet": {"children": {'title': issue['title'], 'date': now}},
                             },
                             upsert=True)

Gives the following result:

{
    "title" : title_1,
    "children" : [ 
        {
            "title" : "title_1",
            "date" : ISODate("2018-07-25T15:55:45.134Z")
        }
    ],
}
Cathal Cronin
  • 1,245
  • 2
  • 13
  • 27
  • Possible duplicate of [Can mongo upsert array data?](https://stackoverflow.com/questions/13588342/can-mongo-upsert-array-data) – Vikash Singh Jul 25 '18 at 15:06
  • I'm not upserting based on an embedded filed of a document. Documents will be created when a new title appears so I don't think it's a duplicate exactly – Cathal Cronin Jul 25 '18 at 15:14
  • This pattern seems counter-intuitive and unnecessarily complex to me. So basically if there are no entries with a specific title yet, it will insert a single document. If later there is another input with the same title, it should be inserted as an array into that same document? What are you trying to achieve using this design? How will the data be queried? – kevinadi Jul 27 '18 at 05:45
  • @KevinAdistambha The record represents a device, the children key is a history of all the times that this device reports itself with an issue. So If a device never reports when it is having an issue, that list would be empty otherwise it would be a list of all the times this device reported an issue. – Cathal Cronin Jul 27 '18 at 07:45

0 Answers0