简体   繁体   中英

Convert MongoDB query to Snowflake

I'm working on a migration project (MongoDB to Snowflake) and trying to convert one of the mongo queries to Snowflake, we have a use case to fetch records if all the elements from an array matched based on the given parameters.

Mongo DB Function: $all
The $all operator selects the documents where the value of a field is an array that contains all the specified elements.

Mongo Query:

db.collection('collection_name').find({
  'code': { '$in': [ 'C0001' ] },
  'months': { '$all': [ 6, 7, 8, 9 ] } --> 6,7,8,9 given parameters
});


Table Structure in snowflake:
column name       datatype
code               varchar(50)
id                 int
months             ARRAY
weeks              ARRAY

Could you provide some suggestions on how to write this query in Snowflake? Any recommendations would be helpful.

You can use ARRAY_SIZE and ARRAY_INTERSECTION to test it:

Here is sample table:

create or replace table test ( code varchar, months array );

insert into test select 'C0002', array_construct( 1, 2, 5,8,6,3)
union all select 'C0001', array_construct( 1,2,3 ) 
union all select 'C0002', array_construct( 2, 12, 3,7,9) 
union all select 'C0001', array_construct( 7,8,9,3, 2) ;

Here is query to test:

select * from test where code in ('C0001','C0002')
and ARRAY_SIZE( ARRAY_INTERSECTION(  months, array_construct( 3, 2 ))) = 2;

So I find the intersection of two arrays, and check the number of items. If you want to look for 3 items, you should set 3 (and it goes like this):

select * from test where code in ('C0001','C0002')
and ARRAY_SIZE( ARRAY_INTERSECTION(  months, array_construct( 3, 2, 7 ))) = 3;

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