The correct way to create a mongoose pattern for a dynamic number of key / value pairs

I have a word count function that returns an object with a set of key value pairs. Something like that:{a: 4, tale: 3, of: 8, two: 3, cities: 3 }

Obviously, this object can have different lengths and have different key / value pairs. I would like to add this data to the "article" in Mongodb. Using mongoose.js is the best scheme I can come up with. I am concerned that mongo creates a ton of Word documents with one key / value pair and id. Is there a better way?

in / models / article.js

mongoose.Schema({
  wordcounts: [wordSchema]
})

in / models / word.js

mongoose.Schema({
  word: String,
  count: Number
})

So, it will look like this:

[{
_id: 3980923urjf0e232902343252,
wordcounts: [
  {_id: 2039840297502938402934, word: "Apple", count:3}, 
  {_id: 20398sdfsdfsdfaaa4asd3, word: "Banana", count:5}
  ]
}]

Is there a better way?

+4
source share
1 answer

, Schema.Types.Mixed (. http://mongoosejs.com/docs/schematypes.html)

, :

mongoose.Schema({
  wordcounts: [{
    word: String,
    count: Number
  }]
})
+4

Source: https://habr.com/ru/post/1615671/


All Articles