How to use an account other than pymongo?

I have sql:

select field2, count(distinct field1) from table group by field2; 

How to achieve this sql in pymongo?

+6
source share
2 answers

You can execute these queries using the pymongo driver using the .group() function.

db.table.group(["field1"], {}, {"count":0},"function(o, p){p.count++}" )

This will group the various "field1" values ​​and increase the counter to track the number of occurrences of each.

+4
source

I found this resource very useful when working on converting from SQL to Mongo SQL to a Mongo map map

In this case, you look like something similar to:

 db.users.group({key: {age: true}, initial: {count: 0}, reduce: function (obj, prev) { prev.count++;} } ) 

Since you have more complex logic, you need to use the reduce function. Honestly, you need to read a little more to fully understand what is happening here. Reduce map

+2
source

Source: https://habr.com/ru/post/903173/


All Articles