Can you give some context as to what "a better way" should do better than your solution?
Sam Mefford Senior Engineer MarkLogic Corporation [email protected] Cell: +1 801 706 9731 www.marklogic.com<http://www.marklogic.com> This e-mail and any accompanying attachments are confidential. The information is intended solely for the use of the individual to whom it is addressed. Any review, disclosure, copying, distribution, or use of this e-mail communication by others is strictly prohibited. If you are not the intended recipient, please notify us immediately by returning this message to the sender and delete all copies. Thank you for your cooperation. ________________________________ From: [email protected] [[email protected]] on behalf of John Muehlhausen [[email protected]] Sent: Tuesday, April 11, 2017 4:08 PM To: [email protected] Subject: [MarkLogic Dev General] Trying to find equivalent to MongoDB aggregate( match -> project[include] -> project[exclude] -> unwind ) I have a MarkLogic solution in server-side JavaScript but I'm thinking there must be a better way? The document is in a bitemporal store called "zzz". I want to do the following: Search documents in both the "zzz" and "latest" collections (i.e. the most recent version of "zzz"). Find the documents where papa="Dan". Of these results, eliminate the "child" arrays from "children" items. Return {yoyo,hoop} documents where there will be as many return documents as there are children. In other words, there is denormalization where yoyo is duplicated. A source document with two children would produce two output documents. JavaScript follows example document: { "systemStart": "2017-04-11T17:46:48.468112Z", "systemEnd": "9999-12-31T11:59:59Z", "validStart": "2014-04-03T11:00:00", "validEnd": "2014-04-03T16:00:00", "id": "2017-04-11/34567", "date": "2017-04-11", "yoyo": "12345", "papa": "Dan", "children": [ { "hoop": "A", "child": [ { "a": "a" } ] } , { "hoop": "B", "child": [ { "a": "b" } ] } ] } My current solution: var query=cts.andQuery([cts.collectionQuery("latest"),cts.collectionQuery("zzz"),cts.jsonPropertyValueQuery("papa","Dan")]); var results=cts.search(query); var arr = Array(); for(var result of results) { result = result.toObject(); var len = result.children.length; for(var i=0;i<len;i++) { delete result.children[i].child; result.children[i].yoyo = result.yoyo; arr.push(result.children[i]); } } arr; Current output in Query Console, assuming two documents are found: [ { "hoop": "A", "yoyo": "12345" } , { "hoop": "B", "yoyo": "12345" } , { "hoop": "A", "yoyo": "12345" } , { "hoop": "B", "yoyo": "12345" } ] In MongoDB the query looks something like this (without the bitemporal of course): db.zzz.aggregate( [ {$match: {“papa” : “Dan”}}, {$project: {_id: false, “yoyo”: true, “children”: true} }, {$project: {“children.child”:false}}, {$unwind: “$children”} ] ) Thanks, John
_______________________________________________ General mailing list [email protected] Manage your subscription at: http://developer.marklogic.com/mailman/listinfo/general
