2016-04-24 10 views
1

Hier ist mein Schema:Mungo - purposly einfügen doppelte Eigenschaften auf das gleiche Dokument

var schema = mongoose.Schema; 

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

Grundsätzlich ist dieses Dokument soll Protokolle auf verschiedenes Projekt aufzunehmen. Jedes Protokoll hat ein Datum, an dem es erstellt wurde, und einen Kommentar zum Projekt selbst. Die Informationen werden über Formulare eingegeben.

Aber hier ist das Problem: Wenn jemand ein weiteres Protokoll zu diesem Dokument hinzufügen wollte, konnten sie nicht; Stattdessen würde ein neues Protokoll einfach das alte Protokoll überschreiben.

Ich möchte wissen, wie jemand, ein Formularfeld in einem Browser verwenden, könnten mehrere Protokolleinträge, so dass in MongoDB es so etwas wie folgt aussieht:

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

Um zur QoP-Antwort hinzuzufügen, checkout [SubDocuments] (http://mongoosejs.com/docs/subdocs.html), ich glaube, dass es das ist, was Sie brauchen. – matt

Antwort

3

Ihr Schema wie folgt

sein sollte
var schema = mongoose.Schema; 

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

Yup, das hat es getan. Die Verwendung eines Arrays ist die richtige Lösung für mein Problem. – MonkeyOnARock

Verwandte Themen