2

Given the following data set:

{ "_id" : ObjectId("510458b188ce1d16e616129b"), "codes" : [ "oxtbyr", "xstute" ], "name" : "Ciao Mambo", "permalink" : "ciaomambo", "visits" : 1 }
{ "_id" : ObjectId("510458b188ce1d16e6161296"), "codes" : [ "zpngwh", "odszfy", "vbvlgr" ], "name" : "Anthony's at Spokane Falls", "permalink" : "anthonysatspokanefalls", "visits" : 0 }

How can I convert this python/pymongo sort into something that will work with the MongoDB aggregation framework? I'm sorting results based on the number of codes within the codes array.

z = [(x['name'], len(x['codes'])) for x in restaurants]
sorted_by_second = sorted(z, key=lambda tup: tup[1], reverse=True)
for x in sorted_by_second:
    print x[0], x[1]

This works in python, I just want to know how to accomplish the same goal on the MongoDB query end of things.

1 Answer 1

3
> db.z.aggregate({ $unwind:'$codes'}, 
                 { $group : {_id:'$_id', count:{$sum:1}}}, 
                 { $sort :{ count: 1}})
Sign up to request clarification or add additional context in comments.

2 Comments

That at least gives me a start. I'd like to have the full results shown, but at least I'm in the right direction. Any clues about the performance of this vs. the application side?
this might get closer: db.z.aggregate({ $unwind:'$codes'}, { $group : {_id:{name:'$name', permalink:'$permalink'}, count:{$sum:1}}}, {$sort:{count:-1}}) Performance? You'll have to test, but chances are your MongoDB server is IO-bound rather than CPU-bound, so spending some CPU doing the aggregation there, instead of filling the wire with documents for processing in Python, is probably a win.

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.