In Views, Avoid Emitting the Document _id posted Wednesday, November 6, 2024 by The Neighbourhoodie CouchDB Team
During our consulting on many many customer CouchDB installations, we keep finding the same common pattern, view map functions that look similar to this:
emit(doc.foo, doc._id)
The reasoning is usually simple, when accessing the view result later in code, people just want to take out the row.value
to do some more work:
const result = await db.query(…)
results.rows.forEach((row) => {
console.log(row.value)
})
So far so good.
But CouchDB already keeps track of a row’s doc _id
and for us. And the query result already includes those values. Let’s examine a result row JSON:
{
"key": "what you had as the first argument in emit()",
"value": "what you had as the second argument in emit()",
"id": "the document _id value"
}
As you can see, the document _id
is already part of each result row, so you don’t have to add it yourself and save yourself some storage costs and gain a small performance improvement.
The only exception to this rule is when you use the Linked Documents feature.