簡體   English   中英

將 MongoDB 查詢轉換為 Snowflake

[英]Convert MongoDB query to Snowflake

我正在處理一個遷移項目(MongoDB 到 Snowflake)並嘗試將其中一個 mongo 查詢轉換為 Snowflake,如果數組中的所有元素根據給定的參數匹配,我們有一個用例來獲取記錄。

Mongo 數據庫函數:$all
$all 運算符選擇字段值是包含所有指定元素的數組的文檔。

蒙戈查詢:

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

您能否就如何在 Snowflake 中編寫此查詢提供一些建議? 任何建議都會有所幫助。

您可以使用 ARRAY_SIZE 和 ARRAY_INTERSECTION 來測試它:

這是示例表:

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) ;

這是要測試的查詢:

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

所以我找到了兩個數組的交集,並檢查項目的數量。 如果要查找 3 個項目,則應設置 3(如下所示):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM