understanding mongoose [Schema.Types.Mixed]

28,716

That schema is fine. Defining an object within an array schema element is implicitly treated as its own Schema object. As such they'll have their own _id field, but you can disable that by explicitly defining the schema with the _id option disabled:

var blogSchema = new mongoose.Schema({
    title: String,
    writing: [new Schema({
        post: String,
        two: Number,
        three : Number,
        four  : String,
        five : [new Schema({ 
            a: String,
            b: String,
            c: String,
            d: String,
            e: { type: Date, default: Date.now }, 
        }, {_id: false})]
    }, {_id: false})],
});
Share:
28,716
cathy.sasaki
Author by

cathy.sasaki

Updated on May 14, 2020

Comments

  • cathy.sasaki
    cathy.sasaki almost 4 years

    Is the below schema defined correctly or does writing need to be writing: [Schema.Types.Mixed] or writing: [{}]?

    That is, if you have an array of dictionaries -- [{},{},{}] -- one can't predefine the internal structure unless you create another schema and embed it. Is that the right interpretation of the docs?

    http://mongoosejs.com/docs/schematypes.html

    var blogSchema = new mongoose.Schema({
      title:  String,
      writing: [{
            post: String,
            two: Number,
            three : Number,
            four  : String,
            five : [{  a: String,
                        b : String,
                        c  : String,
                        d: String,
                        e: { type: Date, default: Date.now }, 
                    }]
      }],
    });
    

    Thanks.

  • cathy.sasaki
    cathy.sasaki almost 11 years
    Okay. That "implicit schema" is a big insight. Thanks. Does that mean then I should be using .populate() to work with these. I've been struggling getting info out of dictionaries in arrays.
  • JohnnyHK
    JohnnyHK almost 11 years
    @cathy.sasaki No, this is a schema for embedded objects. You only use populate when your schema explicitly contains an ObjectId reference to another collection (model).
  • cathy.sasaki
    cathy.sasaki almost 11 years
    @JonnyHK Got it. You are wiz at mongodb, is there is a specific book you recommend? Or did you just poor over the documentation? I'm trying get up to set a foundation of knowledge on this front.
  • JohnnyHK
    JohnnyHK almost 11 years
    @cathy.sasaki I think I started out reading the first edition of this book. It provides a great base of understanding. There's also the free Little MongoDB Book.
  • cathy.sasaki
    cathy.sasaki almost 11 years
    @JonnyHK Wonderful. Thanks for sharing. I'd been circling the o'reilly book. Just ordered it on your rec and downloaded the 31 page pdf Little MongoDB Book to read tonight/tomorrow.
  • stevek-pro
    stevek-pro over 9 years
    This seems to be more related to Mongoose than to Mongo. I find the Mongoose Docs leave a lot to be desired. Is there any book on Mongoose?