0

I need to do some operations on one collection and at the end store the result in another collection. Will be doing this via aggregation. Platform is node, mongoose, mongodb.

One of the required operations which I can't figure out is urlEncoding a particular field. The field can be constrained to only contain alphabets, numbers and spaces, so basically what I need to do is convert spaces to %20

Note that I need to do this while using node. I am pretty new in node, mongoose, mongodb, etc, so I'm not sure If I can run custom javascript code from node into mongoose or if that will be a good idea.

1 Answer 1

1

You can use encodeURIComponent() to url encode a string in javascript.

You could do this on all field values before storing them, but only if you're sure you'll never need the original value (the converse method, decodeuricomponent() may not always honor things such as case).

Alternatively, you could store the value 'as is', and use encodeURIComponent() when retrieving the value.

Sign up to request clarification or add additional context in comments.

1 Comment

The values will be created during the aggregation process. So can't encode while storing. During aggregation, multiple values need to combine to form a url, so can't use encodeURIComponent afterwards either. Unless I reform my whole codebase and database schema, which I'd rather not do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.