create a view that return only a subset of values from a document, each with its key and value within a json string. like if one given view returns a document as this following, Is it possible to get some fields information for a one request? thank you
{
"total_rows":10,
"offset":3,
"rows":[{
"id":"doc1",
"key":"abc123",
"value": {
"_id":"aaaa",
"_rev":"bbb",
"field1":"abc",
"field2":"bcd",
"field3":"cde",
"field4":"123",
"field5":"789",
"field6":"aa@email.com",
"field7":"ttt",
"field8":"iii",
"field9":[{
"field91":"tyui",
"field92":"55555"
}],
"field10"::"0000",
"field11"::"55555",
"field12"::"0030".........
}
}
I just want to create a view that returns some fields only the following:
{
"field1":"abc",
"field2":"bcd",
"field3":"cde",
"field4":"123",
"field5":"789",
"field6":"aa@email.com",
"field7":"ttt",
"field8":"iii",
"field9":[{
"field91":"tyui",
"field92":"55555"
}]
}
Asked
Active
Viewed 2,333 times
2
1 Answers
2
A map function that emits a new document with selected fields only. As an example, let's map field1 (a string) and field9 (an array) only:
function map(doc) {
emit(doc._id, {
field1: doc.field1,
field9: doc.field9
});
}
In the above example, each document will be fired with a key being the original doc ID and the value being the mapped fields you require. This is useful if you are planning to add a reduce function later.
Depending on your use case, you may just want to emit the mapped objects:
function map(doc) {
emit({
field1: doc.field1,
field9: doc.field9
});
}
Please see http://guide.couchdb.org/draft/views.html The documentation on building data views is pretty good, you can discover a lot by experimenting..

seb
- 808
- 1
- 9
- 17
-
Great thanks for your good suggestion and sharing the learning material In this case, if field1 is the key, and I can get the selected 9 fields results like doc.field1 ="aaa", with the following: function map(doc) { emit(doc.field1,{ field1: doc.field1, field2: doc.field2, ..... field9: doc.field9 }); } and call http://...viewName?key="aaa", the results include _id, key,and the value But if I just want to get all the selected fields value with doc.field1 ="aaa", not included like the key and _id, what should I do? – Jamesjin May 20 '16 at 09:37
-
I don't think you can avoid those coming back from CouchDB. But it shouldn't be difficult to 'get rid' of those fields client side, i.e.: map to another object (using javascript I guess, if this is a web app?). It is hard to tell without knowing your use case. – seb May 21 '16 at 09:07
-
thank yo Seb, it is not web app I just do some practice on couchdb futon, I want to 'get rid' of those first fields like _id, key and value from the results:, :{"id":"xxxx","key":"xxxx","value": ............................btw, can I know your email or others that I can get touch with you? – Jamesjin May 24 '16 at 15:35
-
hope you can advise me, why I can not run the results in couchdb like following map, what is wrong? function (doc) { var json_data = doc; json_data = json_data.rows; for (var i = 0; i < json_data.length; i++) { var temp = json_data[i].value; delete temp._id; delete temp._rev; emit(null,{ "field1": temp.field1, "field2": temp.field2, "field3": temp.field3, ..... }); } } – Jamesjin May 25 '16 at 08:03
-
You cannot get rid of the fields in question: id, key, value on couchdb side. You have to do it on your client. Whether the client is a web app served from somewhere else or maybe it is a couch app - does not matter. I don't understand why those field are a problem, could you please explain? – seb May 25 '16 at 10:59
-
Hi Seb,not sure if it is okay to remove the fields _id and _rev through a show function in couchdb? – Jamesjin May 25 '16 at 15:16
-
Show function is meant to create a view (for display) of the data but I guess you can try it that way. Look at the list function too if you are dealing with multiple results. – seb May 25 '16 at 19:12
-
thanks Seb, currently I am studying the couchdb, I can define the display function in the design document in futon? btw, I have question in my local couch db which have some duplication document results from map view results, I want to remove those duplicate, how can do this? thank you http://stackoverflow.com/questions/37453127/is-there-a-way-to-clear-duplication-record-from-results – Jamesjin May 26 '16 at 08:17
-
Hi Seb, how to use reduce that merely takes the first document for any given key to remove duplicate document? stackoverflow.com/questions/37453127/is-there-a-way-to-clear-duplication-record-from-results – Jamesjin May 26 '16 at 22:12
-
Hi Seb, can you help me take a look this map/reduce issue with merging some same values on some fields with some condition, thank you: http://stackoverflow.com/questions/37759830/create-a-view-with-map-reduce-functions-that-return-same-values-with-some-crit – Jamesjin Jun 11 '16 at 04:17