Ich habe einen Datensatz in meiner Sammlung namens Pflanzen geladen. Hier ist ein Beispiel für eine Pflanze:Meteor manipulieren Mongodb Daten von Funktion
{
"_id": "zGdXzfFTAzhrhCvqE",
"Plant": "Carrot",
"Companions": ["Beetroot", "Dandelion", "Rose"]
}
ich brauche, um meine Sammlung zu aktualisieren, so dass jeder Begleiter ist ein Rekord (hat eine _id), so zuerst muß ich überprüfen, ob der Begleiter bereits eine _id hat, aber ich kann‘ t scheint die Syntax richtig zu machen.
//why does'nt this work?
var com = Plants.find({"Plant": "Thyme"});
console.log("id: " + com._id); //returns undefined, even though it exists in the collection
//this works
Plants.find({}).forEach(function(plant){
var companions = plant.Companions;
console.log(companions[0]); //prints out the first plantname in the array
//here I need to check if the plant is already in the collection
for(var i = 0; i < companions.length; i++){
var com_plante = Plants.findOne(companions[i]);
//this writes out undefined
console.log("com_plante: " + com_plante._id + " " + com_plante.Plant);
}
}
Was ist falsch an meiner Syntax?
Danke. Ich bin noch ein Neuling ... – Heidi