I generate trigram snippets as primary keys. The field words is an array of terms represented by the trigram key, e.g.:
{
"trigram": "#ha",
"words": ["hahaha", "harley", "mahalo"]
}
The problem is pushing new terms to the array. I don't know how to use $addToSet for this.
db["Terms"].update({
"trigram": trigram,
{"$addToSet": {"words":word}
})
It should append word to the words field. But the database remains empty without returning any error messages.
What should I do?
Unless you use the upsert option, an update will only modify existing docs, not create them. Try this instead:
db["Terms"].update(
{ "trigram":trigram },
{ "$addToSet":{"words":word} },
upsert=True)
By using the upsert option, it will create the doc if missing, otherwise just update the existing one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With