javascript - In mongoose how to iterate over array of ObjectId? -
I try to rephire from Javascript object for the Mongoose object. I have two simple models:
Candidate model:
var candidateSchema = new schema ({name: string, score: {type: Number, default: 0, minimum: -2, max: 2}}); CandidateSmama.method.updateCor = function updateScore (NewScore) {this.score = newScore; }; Vote Model with a list of candidates: var voteSchema = new schema ({candidate: [type: schema type object, referee: 'candidate'}] }); Function candidates are already (candidates, new Candidates) before {var alreadyExists = false; For candidates. Each (job (candidate) {if (newCandidate.name === candidate name} {first Axis = true;}}); Will return in advance; } VoteSchema.methods.addCandidate = function addCandidate (newCandidate, callback) {var candidates = this.candidates; If (Candidates already candidates (candidates, new Candidates)) {candidates.push (newCandidate); } This.save (callback); };
I try to add a new candidate in my opinion, only if it does not exist.
I can not run again on my candidates (For the candidates, each (task (candidate) {) because it is an array of _id and does not have an array of javascript objects
Does anyone have an idea?
Mongodybi It would be better to use.
voteSchema.methods .addCandidate = addCandidate function (new call, callback) {this.candidates.addToSet (newCandidate._id); this.save (callback) ;}
Comments
Post a Comment