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")
}
],
}