[SERVER-150] Javascript closures Created: 12/Jul/09 Updated: 17/Feb/17 Resolved: 17/Feb/17 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Shell |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Minor - P4 |
| Reporter: | sandstrom | Assignee: | DO NOT USE - Backlog - Platform Team |
| Resolution: | Done | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
OS X 10.5 |
||
| Backwards Compatibility: | Fully Compatible |
| Participants: |
| Description |
|
If I'm not misstaken diff should be accessible within the keyf function, due to closures. However, I need to place it within the keyf function for the shell to recognise it. Is this a bug perhaps? Result without diff within the keyf function:
Result with diff within the keyf function: , {"day" : 1 , "month" : 5 , "year" : 2009 , "count" : 0}, {"day" : 26 , "month" : 5 , "year" : 2009 , "count" : 0}] FUNCTION var now = new Date(); var res = db.group({ , return { "day":date.getDate(), "month":date.getMonth(), "year":date.getFullYear() }; , , , |
| Comments |
| Comment by Mira Carey [ 17/Feb/17 ] |
|
Making this change would be backwards breaking due to the extant server side environment (from sources like system.js) and as such we won't be able to consider this feature at this time. |
| Comment by Timo Mika Gläßer [ 19/Sep/10 ] |
|
You should look into the documentation under "scope". db.runCommand( { mapreduce : <collection>, map : <mapfunction>, reduce : <reducefunction> [, query : <query filter object>] [, sort : <sort the query. useful for optimization>] [, limit : <number of objects to return from collection>] [, out : <output-collection name>] [, keeptemp: <true|false>] [, finalize : <finalizefunction>] [, scope : <object where fields go into javascript global scope >] [, verbose : true] }); This can probably be used to "mimic" the behaviour you expect. Unfortunately the scope is not copied. I guess this has to do with shipping the functions or so? |
| Comment by sandstrom [ 13/Jul/09 ] |
|
It's not in the documentation. I pasted everything into the shell and ran it. |
| Comment by Eliot Horowitz (Inactive) [ 13/Jul/09 ] |
|
That would work if the function was being evaluated locally, but the closure isn't sent over automatically. |