简体   繁体   中英

Including dict as value in MongoDB mapReduce emit

For a map function, I have the following code that works:

self.url_with_sessions_mapper = Code("""
    function(){
        emit(this.page, {'session':this.session, 'time':this.time});
    }
    """)

But for some reason, this doesn't:

self.url_with_sessions_mapper = Code("""
    function(){
        emit(this.page, {this.session:this.time});
    }
    """)

I get this error message

on namespace test_database.$cmd failed: exception: SyntaxError: Unexpected token.

Why wouldn't the latter syntax work?

Turns out the keys in a dictionary ('associative array') are always being interpreted as strings, never as variable names. To achieve what I was aiming for, the syntax is:

    self.url_with_sessions_mapper = Code("""
        function(){
            var dd = {};
            dd[this.session] = this.time;
            emit(this.page, dd);
        }
        """)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM