2017-06-09 6 views
1

Ich bin hier fest, um Mungo Schema für Happenings und Geburtstage Felder zu schreiben. Ich benutze mongodb/mongoose und möchte den ähnlichen Inhalt in Ereignissen Ansammlungen speichern.Mongoose Schema für Arrays und Hashes

{ 
"_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" 
    } 
} 

}

Das Schema I versucht

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


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

Wie soll ich das obige Schema ändern ??

Antwort

1

Nach Ihrem Dokument denke ich, Ihr Schema wie

könnte
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);