Um das klarzustellen, sucht der Endkey-Parameter nicht nach einem Suffix. Startschlüssel und Endschlüssel sind wie die Grenzen der Schlüssel zu bekommen. Beispielsweise könnten Sie das Dokument mit der ID 1 an die ID 10 startkey="1"&endkey="10"
abrufen.
In Ihrem Fall möchten Sie eine Ansicht erstellen, die Ihre Dokumente nach ihrem Domänennamen gruppiert. Ich habe ein Design-Dokument mit einer byDomain
Ansicht erstellt. Die Abbildungsfunktion sieht wie folgt aus:
function(doc){
if(doc.email){ //I used the document's property email for my view.
//Now, we will emit an array key. The first value will be the domain.
//To get the domain, we split the string with the character '@' and we take what comes after.
//Feel free to add more validations
//The second key will be the document id. We don't emit any values. It's faster to simply add
//the includes_docs query parameter.
emit([doc.email.split('@')[1],doc._id]);
}
}
- Lassen Sie uns alle meine Dokumente abfragen, um Ihnen zu zeigen, was ich
Anfrage haben: http://localhost:5984/test/_all_docs?include_docs=true
Antwort:
{"total_rows":4,"offset":0,"rows":[
{"id":"7f34ec3b9332ab4e555bfca202000e5f","key":"7f34ec3b9332ab4e555bfca202000e5f","value":{"rev":"1-c84cf3bf33e1d853f99a4a5cb0a4af74"},"doc":{"_id":"7f34ec3b9332ab4e555bfca202000e5f","_rev":"1-c84cf3bf33e1d853f99a4a5cb0a4af74","email":"[email protected]"}},
{"id":"7f34ec3b9332ab4e555bfca202001101","key":"7f34ec3b9332ab4e555bfca202001101","value":{"rev":"1-53a8a9f2a24d812fe3c98ad0fe020197"},"doc":{"_id":"7f34ec3b9332ab4e555bfca202001101","_rev":"1-53a8a9f2a24d812fe3c98ad0fe020197","email":"[email protected]"}},
{"id":"7f34ec3b9332ab4e555bfca202001b02","key":"7f34ec3b9332ab4e555bfca202001b02","value":{"rev":"1-cccec02fe7172fb637ac430f0dd25fa2"},"doc":{"_id":"7f34ec3b9332ab4e555bfca202001b02","_rev":"1-cccec02fe7172fb637ac430f0dd25fa2","email":"[email protected]"}},
{"id":"_design/emails","key":"_design/emails","value":{"rev":"4-76785063c7dbeec96c495db76a8faded"},"doc":{"_id":"_design/emails","_rev":"4-76785063c7dbeec96c495db76a8faded","views":{"byDomain":{"map":"\t\tfunction(doc){\n\t\t\tif(doc.email){ //I used the document's property email for my view.\n\t\t\t\t//Now, we will emit an array key. The first value will be the domain.\n\t\t\t\t//To get the domain, we split the string with the character '@' and we take what comes after.\n\t\t\t\t//Feel free to add more validations\n\t\t\t\t//The second key will be the document id. We don't emit any values. It's faster to simply add\n\t\t\t\t//the includes_docs query parameter.\n\t\t\t\temit([doc.email.split('@')[1],doc._id]); \n\t\t\t}\n\t\t}"}},"language":"javascript"}}
]}
Wie Sie sehen können Ich habe wenige minimalistische Dokumente mit der Eigenschaft "email" eingestellt.
- wir meiner Ansicht nach Abfrage ohne Parameter
Anfrage: http://localhost:5984/test/_design/emails/_view/byDomain
Antwort:
{"total_rows":3,"offset":0,"rows":[
{"id":"7f34ec3b9332ab4e555bfca202001101","key":["example.com","7f34ec3b9332ab4e555bfca202001101"],"value":null},
{"id":"7f34ec3b9332ab4e555bfca202000e5f","key":["gmail.com","7f34ec3b9332ab4e555bfca202000e5f"],"value":null},
{"id":"7f34ec3b9332ab4e555bfca202001b02","key":["gmail.com","7f34ec3b9332ab4e555bfca202001b02"],"value":null}
]}
- Lassen Sie uns nur Dokumente mit der gmail.com-Domain abfragen.
Anfrage: http://localhost:5984/test/_design/emails/_view/byDomain?startkey=["gmail.com"]&endkey=["gmail.com","\ufff0"]
Ergebnis:
{"total_rows":3,"offset":1,"rows":[
{"id":"7f34ec3b9332ab4e555bfca202000e5f","key":["gmail.com","7f34ec3b9332ab4e555bfca202000e5f"],"value":null},
{"id":"7f34ec3b9332ab4e555bfca202001b02","key":["gmail.com","7f34ec3b9332ab4e555bfca202001b02"],"value":null}
]}
Danke, weißt du, wie kann ich ein Ergebnis zu erhalten, die in absteigender Reihenfolge des „Wert“ Wertes sortiert wird? http://stackoverflow.com/questions/38800308/how-can-i-get-a-map-reduce-result-which-is-sorted-within-descending-order-of-the – Jamesjin
@Jamesjin Es ist nicht einfach Weg. Auch würde die Antwort, die ich zur Verfügung stellen würde, [dem] (http://stackoverflow.com/questions/2817703/sorting-couchdb-views-by-value) sehr ähnlich sein. –