数组的Mongoose模式&哈希

时间:2017-06-09 07:01:44

标签: mongodb mongoose mongoose-schema

我被困在这里为发生事件生日字段编写mongoose架构。我正在使用mongodb / mongoose,并希望在事件集合中保存类似的内容。

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

}

我试过的架构

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


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

我应该如何修改上述架构?

1 个答案:

答案 0 :(得分:1)

根据您的文档,我认为您的架构可能就像

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);