Using Mongoose, I do not believe that there is a way to do what you describe. To explain, let's take an example where your keys are dates and values โโare high temperatures to form pairs like {"2012-05-31": 88}.
Take a look at the structure you are proposing:
{ // meta information subdocs: { "2012-05-30" : 80, "2012-05-31" : 88, ... "2012-06-15": 94, } }
Since you must predefine the schema in Mongoose, you must know your key names in advance. In this case, we probably would not know in advance what dates we are collecting data, so this is not a good option.
If you are not using Mongoose, you can do it without any problems. MongoDB itself is highlighted when inserting values โโwith new key names into an existing document:
> db.coll.insert({ type : "temperatures", subdocuments : {} }) > db.coll.update( { type : "temperatures" }, { $set : { 'subdocuments.2012-05-30' : 80 } } ) > db.coll.update( { type : "temperatures" }, { $set : { 'subdocuments.2012-05-31' : 88 } } ) { "_id" : ObjectId("5238c3ca8686cd9f0acda0cd"), "subdocuments" : { "2012-05-30" : 80, "2012-05-31" : 88 }, "type" : "temperatures" }
In this case, adding Mongoose on top of MongoDB takes away some of MongoDBโs own flexibility. If your use case fits well with this MongoDB feature, then using Mongoose might not be the best choice.
source share