couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Cesar Delgado <>
Subject Re: Adding a member into a JSON object in map function
Date Wed, 14 Apr 2010 02:09:20 GMT

Thanks for the reply, it works perfectly.   I'm more interested in doing some very common
transformation on the data I have automatically while pushing the creation far-far away from
users.  Just seem like these map functions are a great way to do the work that needs to be
done anyway.  Plus, if I understand it correctly, the documents that are created are cached
so these objects, once computed, should be very fast.

I'll check out the link you sent me for more examples for other approaches.

Thanks for the help!


From: J Chris Anderson <>
Sent: Tue, April 13, 2010 9:43:14 AM
Subject: Re: Adding a member into a JSON object in map function

On Apr 12, 2010, at 10:42 PM, Cesar Delgado wrote:

> I'm playing around with CouchDB and am kinda curious if I can add a new member to an
object in the map (and/or reduce I guess as well) part.  
> Ok, so an example is I have an object with a name and a plain-text password:
> {"name":"cesar", "passwd":"insecure"}
> What I would like to do is run a map function on this that computes the md5 and then
inserts it into the object. I know it's horrible practice and I know it's wrong and I know
this is bad.  It's an example so please don't flame.  

I think you are saying you'd like to mutate the document itself from the map function. You
don't really want to do this. You can do something like this.

function(doc) {
var hash = md5(doc.password);
  doc.hash = hash;
delete doc.password;
emit(doc._id, doc)

this will give you a value that is the whole doc but with the hash instead of the cleartext.

If you are trying to keep cleartext out of your database, you can also use an _update function,
which can intercept updates before writing the doc to the database. See:

for examples.


> Right now all I've been able to do is return the value as a value and the doc._id as
the key.  
> function(doc) {
> var hash = md5(doc.passwd) ;
> emit (doc._id,hash) ;
> }
> I'd love to just have a new JSON oabject as the value and no key.
> function(doc) {
> var hash = md5(doc.passwd) ;
>  doc.insertmefunct("hash",hash) ;
>  emit(null,doc) ;
> }
> would produce: 
> {"name":"cesar", "passwd":"insecure","hash":"f166786326d565962e8523266c0e1d57"}
> Thanks for the help,
> -Cesar

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message