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

Reply via email to