Creating a dictionary schema using Mongoose

I am trying to save a dictionary of objects using Mongoose. Realizing that I had lost the detection of changes to save using a mixed type, I was hoping I could create a schema that does not require a mixed type.

There are many examples of creating schemas for arrays of objects, but not dictionaries of objects. Can this be done?

Format:

{ ObjectId : { "attempts" : { "response" : String, "timestamp" : Date }, "complete" : Boolean } } 
+7
javascript mongodb mongoose
source share
2 answers

The mongoose dictionary has no dictionary support. The field name cannot be dynamic in the schema. You have to go with an unprocessed object (embedded document) and implement it like a dictionary. But there will be no confirmation from mongoose and use markModified before saving whenever you change this field

 var fooSchema=mongoose.Schema({name:String, dic:{}}); //dic is your dictionary 
+9
source share

You can have your dictionary like this if meta strong> is a dictionary:

 var UserSchema = new Schema({ meta: { votes: Number, favs: Number } first_name: String, last_name: String, profile_pic: String, gender: String, timezone: Number date: { type: Date, default: Date.now }, updated: { type: Date, default: Date.now }, country: {id: Number, name: String} }); 
0
source share

All Articles