Question

I am using a mongodb collection to store frequencies of words which are near other words like this:

{ word : "a", neighbours : [ { neighbour : "x", frequency : 1 }, { neighbour : "y", frequency : 2 } ] }
{ word : "b", neighbours : [ { neighbour : "y", frequency : 1 }, { neighbour : "z", frequency : 3 } ] }

Each document has a word and a list of subdocuments called neighbours (the nearby words). Each subdocument consists of a neighbour and a frequency of how often that neighbour is found to occur near the word.

What I want to do is to update the list of neighbours for a given word. The problem is that the update can be:

  • A new neighbour, in which case a push has to be made
  • Or an existing neighbour, in which case a increment to the frequency of the respective subdocument has to be made

What is the most efficient way to do this? At the moment I am first checking if the neighbour exists using a find, then do different updates depending on the result.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top