4

I am porting a program written in server side javascript to python using pymongo. The javscript program uses this syntax:

db.dbname.find(pattern1,pattern2).map( function(i){functionname(i) })

Pattern1 and pattern2 are valid mongodb query patterns. Functionname is a valid javascript function. All are defined in the javascript source file. I have searched the documentation but can't seem to find a pymongo find().map function (as opposed to map_reduce.)

How would this be re written in python?

alecxe
476k127 gold badges1.1k silver badges1.2k bronze badges
asked Mar 30, 2014 at 0:09

1 Answer 1

2

You can just see what is map function in javascript doing. Map is a part of functional programming, but what it is doing can be simply described as follows: it is taking an array and modify every element in an array based on the function provided. You can think about it as a loop.

So you need to do the same loop in python. With comprehensions it will be something like this:

[functionname(i) for i in resultFromMongo]

Checkout this for a reference.

answered Mar 30, 2014 at 0:22
Sign up to request clarification or add additional context in comments.

1 Comment

the map function in mongodb works in database side and the map function in python works in application side. so using mongodb's map is so much efficient than python's map function (though it doesn't have significant difference in small amount of data)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.