this first post. please kind.i attempting retrieve distinct data based on keys. first schema:
var sampleschema = mongoose.schema({ name: {type: string, index: true}, _atid: {type: mongoose.schema.objectid, ref: athandler.arraytypes.schema, index: true}, factors: [{ key: {type: string, index: true}, value: {type: string, index: true} }] });
for sample stored, there 0 many factors. have use case need retrieve keys distinct , each key, distinct values.
a desired result like:
{ [ { key: 'key1', values: [values] }, { key: 'key2', values: [values] }, ] }
either doing in mongoose (preferred) or combination of mongoose/nodejs helpful.
i have tried:
- promise.all()
- mongoose aggregation
- mongoose distinct query query key
i have search stackoverflow , haven't come across similar questions or close enough me piece them...if find some, appreciate too...
edit: here 1 failed attempt.
.get('/:sgv/factors', function(req, res){ // factors associated sgv since client cannot work data different sgvs // need distinct factors.key , each collection of distinct values... var sgv = sanitize(req.params.sgv); log.debug('get-samples-[%s]-factors', sgv); samplehandler.samples .distinct('factors.key') .lean(true) .exec() .then(function (factors) { function getvaluesforkey(key){ return samplehandler.samples .distinct('factors.value', { 'factors.key': key}) .exec() .then(function(results){ var p = new promise(function(fullfill, reject){ fullfill(results); }); console.log(results); return p; }) } function getvaluesforkeys(keys){ return promise.all(keys.map(getvaluesforkey)); } getvaluesforkeys(factors) .done(function(results){ // console.log(results); res.status(200).send({keys: keys, values:results}); }, function(err){ console.log(err); res.status(400).send({err: err}); }); }) .catch(function (err) { console.log('error: ' + err); res.status(400).send({err: err}); }); })
the results of follows, there worker died (using cluster). there isn't stack trace or error messages.
['blood','child','father','mother','tumor','433333','4444','4477hhjj','54','555','f2','m2','13','666','m1' ] [ 'blood', 'child', 'father', 'mother', 'tumor', '433333', '4444', '4477hhjj', '54', '555', 'f2', 'm2', '13', '666', 'm1' ] [ 'blood', 'child', 'father', 'mother', 'tumor', '433333', '4444', '4477hhjj', '54', '555', 'f2', 'm2' ] [ 'blood', 'child', 'father', 'mother', 'tumor' ] [ '433333', '4444', '4477hhjj', '54', '555', 'child', 'f2', 'm2' ] [ '433333', '4444', '4477hhjj', '54', '555', 'child', 'f2', 'm2' ] [ '433333', '4444', '4477hhjj', '54', '555', 'child', 'f2', 'm2' ] [ '433333', '4444', '4477hhjj', '54', '555', 'child', 'f2', 'm2' ] [ '433333', '4444', '4477hhjj', '54', '555', 'child', 'f2', 'm2', '13', '566', 'm1' ]
the keys are: ['f1','f2','f3','f4','f5','f6','f8','joker'] above values same each key. not samples (schema) have same number of keys...
i tried promise.all. less familiar promise nodejs must admit, still learning.
so not looking if else comes better answer, great. if so, change answer theirs...
.get('/factors', function(req, res) { // factors associated sgv since client cannot work data different sgvs // need distinct factors.key , each collection of distinct values... log.debug('get-samples-factors'); samplehandler.samples .distinct('factors') .lean(true) .exec() .then(function(data) { var d = []; (var = 0; < data.length; i++) { if (!d[data[i].key]) { d[data[i].key] = new set(); } d[data[i].key].add(data[i].value); } var f = []; (var key in d) { if (d.hasownproperty(key)) { f.push({ key: key, values: array.from(d[key]) }); } } res.json(f); }) .catch(function(err) { console.log('error: ' + err); res.status(400).send({ err: err }); }); })
that took input data mongo show above , collapsed down following, keep in mind have not tested yet verify various combinations of same , different values across samples...:
[{ "key": "f1", "values": ["blood", "tumor", "m2", "13"] }, { "key": "f4", "values": ["child"] }, { "key": "f2", "values": ["father", "mother", "f2", "m1"] }, { "key": "f3", "values": ["mother", "father", "child"] }, { "key": "f7", "values": ["433333"] }, { "key": "f5", "values": ["4444"] }, { "key": "f8", "values": ["4477hhjj"] }, { "key": "f6", "values": ["54"] }, { "key": "joker", "values": ["555", "666"] }]