javascript - Matching collection items by id -
relations between 2 mongo collections.
i have 2 collections: entries , labels:
// example entry { "_id": "4zcbv5oglstffxpvp", "title": "some other title", "entrylabels": [ "tfryveujpqbmrywez", "rn8z57nseyse7nyto", "rn8z57nseyse73yto" ], "author": "tmvirl8otm3zsddst", "createdat": "2016-01-16t15:21:43.153z", "date": "2016-01-16t15:21:43.153z" } // example label { "_id": "9ncnpgh8f5mwnzjka", "color": "#333", "name": "grey label", "author": "tmvirl8otm3zsddst" }
as it's possible have multiple labels entry, keep label ids in entrylabels field. plan save label ids , query labels collection based on that.
this ok if have single labelid because can query labels collection based on id , return label. have array of ids , have return multiple labels.
my attempt this(in template helper) :
labels: function(event){ var entrylabels = this.entrylabels; var arr = [] for(i = 0; < entrylabels.length; i++) { var label = entrylabels[i]; var thelabel = labels.find({_id: entrylabels[i]}).fetch(); arr.push(thelabel); } return arr; }
then, in template:
{{#each labels}} {{name}} <p>asd</p> {{/each}}
this returns 'asd' 3 times cannot access label properties name
so, guess question is, how can access properties. also.. doing right way?
am doing right way?
i no. mongo other database, can retrieve multiple documents @ time. why fetch 1 @ time in for-loop
when can do
labels: function(event){ var entrylabels = this.entrylabels; var arr = labels.find({_id: {$in: entrylabels}}).fetch(); return arr; }
this fix "i cannot access label properties name" problem, because old arr
array of array of documents, not array of documents expected.
Comments
Post a Comment