db.tbl.aggregate(pipeline):
pipeline = [{$match: {"root_field.nested_fld.nested_fld.target_flg" : target_value}}, {$out: "sam_raw"}]
This like SQL query:
create table sam_raw as select * from tbl where target_flg=target_value
db.mycol.update({}, {"$set": {"new_filed" : "new value"}}, {"upsert":"false", "multi" : "true"})
col.update_many({}, {"$set": {"new_filed" : "new value"}}, upsert = False)
Looks python pipeline syntax with extra double quotation also works for Mongo directly.
| Purpose | Mongo | SQL | python: pymongo | rust? |
|---|---|---|---|---|
| Load JSON array | |
|
||
| append one collection to another | db.this_col.aggregate([{"$merge" : {"into" : "appended_col", "on" : "_id"}}]) |
insert into appended_tbl select * from this_tbl |
pipeline = [{"$merge" : {"into" : "appended_col", "on" : "_id"}}]; this_col.aggregate(pipeline) | |
| Frequency based on fields | db.txn.aggregate([{$group : {_id : "$root_field.nested_fld.target_fld", num_txn : {$sum : 1}}}]) |
select target_fld, count(*) as num_txn from schema.tbl group by 1 |
pipeline = [{"$group" : {"_id" : "$root_field.nested_fld.target_fld", "num_txn" : {"$sum" : 1}}}]; rst=list(col.aggregate(pipeline)) |
|
db.txn.aggregate([{$group : {_id : "$root_field.nested_fld.target_fld", num_txn : {$sum : 1}}}, {$out: "freq_target_fld"}]) |
create table freq_target_fld as select target_fld, count(*) as num_txn from schema.tbl group by 1 |
pipeline = [{"$group" : {"_id" : "$root_field.nested_fld.target_fld", "num_txn" : {"$sum" : 1}}}, {"$out": "freq_target_fld"}]; col.aggregate(pipeline) |
||
| check duplication | NA? | |||
| check key field duplication | |
select fld1, fld2, fld3, fld4, count(*) from schema.tbl group by 1,2,3,4 having count(*)>1 |
mycol.aggregate(pipeline) |
|
| get subset of documentation: one to one | ||||
| get subset of documentation: one to multiple |