json - RethinkDB filter on nested objects -
i need solution filter data nested objects. so, json data:
{ "create_datetime": 1431000977 , "creator": { "company": { "id": 0 , "name": "some name" } , "manager": { "id": 0 , "name": "" } } , "finished_datetime": 1431615600 , "id": "00949296-cbea-4d4a-a780-7c8d918a7fd6" , "participants": [ ], "status": "created" , "tender_categories": [ 1285 ] , "views": [ ] }, { "create_datetime": 1431416740 , "creator": { "company": { "id": 70922233 , "name": "some company name" } , "manager": { "id": 1003546168 , "name": "some manager name" } } , "finished_datetime": 1432857600 , "id": "28e0936b-84e0-4ffc-9ad1-78a1d34e9033" , "participants": { "788190": { "creator": { "company": { "id": 788190 , "name": "company name" } , "manager": { "id": 1003546168 , "name": "manager name" } } , "dt_applied": 1431416778 , "viewed": false } } , "status": "created" , "tender_categories": [1303] , "views": [788190] }
i need select 1 record json have participants, not viewed. wrote lot of code, 1 one work.
r.db('test').table('tenders').filter(function(tender) { return tender('participants').coerceto('array').map(function(participant) { return participant('viewed').eq(false) }); });
and
r.db('pm').table('b2b_tenders').map(function(tender) { return tender('participants').filter(function(key) { return tender(key)('viewed').eq(false) }); });
and one. pls one.
any type of filtering nested objects doable. in case, seems want documents, participants have property view
set false.
here's long, yet complete , safe way of checking this:
r.db('test').table('tenders') // documents `participants` property .hasfields('participants') // documents `participants` property object .filter(function (row) { return row('participants').typeof().eq('object') }) // documents participants have `viewed` property .filter(function (row) { return row('participants').coerceto('array') .map(function (row) { return row(1).hasfields('viewed') }) .distinct() .eq([true]) }) // documents participants have `viewed` property set `true` .filter(function (row) { return row('participants').coerceto('array') .map(function (row) { return row(1)('viewed').eq(false) }) .distinct() .eq([true]) })
you can take out or change parts of query fit needs , depending on how know documents coming in. but, query shows how deal nested properties.
Comments
Post a Comment