gb_spectrum
gb_spectrum

Reputation: 2301

Mongoose - purposly insert duplicate properties onto the same document

Here is my schema:

var schema = mongoose.Schema;

var projectSchema = new schema ({
projectName: {type: String},
event: {
    log: {
        recordDate: {type: Date},
        comment: {type: String}
    },
}
});

Basically this document is designed to record logs on various project. Each log has a date it was made, and a comment about the project itself. The information will be inputted through forms.

But here is the problem: If someone wanted to add another log to this document, they couldn't; what would instead happen is that any new log would simply overwrite the old log.

I want to know how someone, using a form field in a browser, could make multiple log entries so that in MongoDB it looks something like this:

{
"_id" : ObjectId("570f8459196a3a301638b18b"),
"projectName" : "project1",
"event" : {
    "log" : { 
         "recorDate" : "2015/04/28"
         "comment" : "The project needs improvement"
        }
     "log" : { 
         "recorDate" : "2015/04/29"
         "comment" : "Project is better"
        }
      "log" : { 
         "recorDate" : "2015/04/30"
         "comment" : "Needs more testing"
        }
}
}

Upvotes: 1

Views: 43

Answers (1)

QoP
QoP

Reputation: 28397

Your schema should be like this

var schema = mongoose.Schema;

var projectSchema = new schema ({
projectName: {type: String},
event: [ {
    log: {
        recordDate: {type: Date},
        comment: {type: String}
    }
   }]
});

Upvotes: 3

Related Questions