Skip to content

Instantly share code, notes, and snippets.

@bpluly
Last active February 11, 2022 15:46
Show Gist options
  • Save bpluly/f64463fa89bd8b47f77aec8ea5445263 to your computer and use it in GitHub Desktop.
Save bpluly/f64463fa89bd8b47f77aec8ea5445263 to your computer and use it in GitHub Desktop.
Couchdb example of using one document to index others, for alternate orders
The order document specifies different orders of the same set of documents.
{
"_id": "order",
"_rev": "11-f5ddfed421c4cfcd24ab38ae74e34723",
"zettelNoteType": "order",
"order": [
{
"orderKey": "bpluly",
"orderList": [
"00000000-150d3af6-8e17-46e5-8da5-4bf7935e52ac",
"00000000-18461272-bff0-4d41-af5e-847d90fef30f",
"00000000-33ed3de9-d2a3-4a9e-a7bc-d72528e9ecf9",
"00000000-41fd198e-7998-47b6-9fa7-1a8d6b122893",
"00000000-453acc27-1306-471e-96ab-9c6260bc5704",
"00000000-485905e6-d12b-46b1-b5bd-216a3cdb917d",
"00000000-82dc2590-2042-4d2b-902c-5a70b67268f7",
"00000000-a2e88c0f-8222-42ef-9090-24330d013fd4"
]
},
{
"orderKey": "zettel_io",
"orderList": [
"00000000-82dc2590-2042-4d2b-902c-5a70b67268f7",
"00000000-41fd198e-7998-47b6-9fa7-1a8d6b122893",
"00000000-150d3af6-8e17-46e5-8da5-4bf7935e52ac",
"00000000-18461272-bff0-4d41-af5e-847d90fef30f",
"00000000-33ed3de9-d2a3-4a9e-a7bc-d72528e9ecf9",
"00000000-453acc27-1306-471e-96ab-9c6260bc5704",
"00000000-485905e6-d12b-46b1-b5bd-216a3cdb917d",
"00000000-a2e88c0f-8222-42ef-9090-24330d013fd4"
]
}
]
}
The view joins the order document to the documents in the lists.
function (doc) {
if (doc.zettelNoteType=="order"){
for (var i in doc.order){
for (var j in doc.order[i].orderList){
emit([doc.order[i].orderKey], {_id: doc.order[i].orderList[j]});
}
}
}
}
@bpluly
Copy link
Author

bpluly commented Feb 11, 2022

And the answer thanks to @rnewson is to use startkey= and endkey= as in:
startkey=["zettel_io"]&endkey=["zettel_io"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment