You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Cesar Delgado <be...@ymail.com> on 2010/04/13 04:45:57 UTC

Is there any way of adding a new member to an object in the map function?

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.  

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


      

Re: Is there any way of adding a new member to an object in the map function?

Posted by Ning Tan <ni...@gmail.com>.
On Mon, Apr 12, 2010 at 10:45 PM, Cesar Delgado <be...@ymail.com> 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.
>
> 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"}

Or, of you want the value to be exactly like the above, do:

var value = { name: doc.name, password: doc.password, hash: hash };
emit(null, value);

Re: Is there any way of adding a new member to an object in the map function?

Posted by Ning Tan <ni...@gmail.com>.
On Mon, Apr 12, 2010 at 10:45 PM, Cesar Delgado <be...@ymail.com> wrote:

> 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"}

doc is just a plain old JavaScript object:

   doc.hash = hash;

should do the trick. Of course, this is only a view output and is not
modifying the original document in the database.