Sam
Sam

Reputation: 5250

Mongoose schema for arrays & hashes

I am stuck here to write mongoose schema for happenings and birthdays fields. I am using mongodb/mongoose and would like to save the similar contents in events collections.

{
"_id" : ObjectId("5938fc171dfe0f225902d85d"),
"month" : 6,
"date" : 9,
"happenings" : [ 
    {
        "incident" : "Muhammad, the founder of Islam and unifier of Arabia, died.",
        "year" : "632"
    }, 
    {
        "incident" : "The Army of the Potomac defeats Confederate forces at Battle of Cross Keys, Virginia..",
        "year" : "1862"
    }, 
    {
        "incident" : "Israeli airplanes attack the USS Liberty, a surveillance ship, in the Mediterranean, killing 34 Navy crewmen..",
        "year" : "1967"
    }, 
    {
        "incident" : "Gemini astronaut Gene Cernan attempts to become the first man to orbit the Earth untethered to a space capsule, but is unable to when he exhausts himself fitting into his rocket pack.",
        "year" : "1966"
    }
],
"birthdays" : {
    "actor" : {
        "name" : "Josh Pence",
        "yob" : 1982,
        "birthplace" : "Santa Monica, CA",
        "role" : "MOVIE ACTOR",
        "image" : "josh_1982.png"
    },
    "actress" : {
        "name" : "Julianna Margulies",
        "yob" : 1966,
        "birthplace" : "Spring Valley, NY",
        "role" : "TV ACTRESS",
        "image" : "julianna_1966.png"
    },
    "player" : {
        "name" : "Julianna Margulies",
        "yob" : 1987,
        "birthplace" : "Ohio",
        "role" : "FOOT BALL",
        "image" : "julianna_1966.png"
    }
}

}

The schema I tried

var schema = new Schema({
   day: Number,
   Month: Number,
   birthdays: Schema.Types.Mixed,
   happenings: [],
   incident:   [String],
   year:   [Number],
})


var event= mongoose.model('Event', schema);

How should I modify the above schema??

Upvotes: 1

Views: 291

Answers (1)

Shaishab Roy
Shaishab Roy

Reputation: 16805

According to your document I think your schema could be like

var schema = new Schema({
  day: Number,
  Month: Number,
  birthdays: Schema.Types.Mixed,
  happenings: [{
    incident: String,
    year: String,
    _id: false
  }]
});

var event= mongoose.model('Event', schema);

Upvotes: 1

Related Questions