在mongoshell脚本中设置变量以检索数据

问题描述

我想动态设置从第一个查询中检索到的“ item”的值到聚合查询中;我尝试了以下方法,但没有成功。

一个查询有效,第二个查询不检索任何数据。

目标是从查询之一中检索一些字符串值,并将其用于第二个查询中的过滤器。

我无法在聚合步骤中“扩展”变量“ item”的值。

// The first query works correctly:
var value_fields_cursor=db.getCollection("collection_one").find(
{
   code: "code_one"
},{_id: 0,"field_conf.value_fields": 1}
 );


var field_name = "";

while (value_fields_cursor.hasNext()) {
   field_name.field_conf.value_fields.forEach((item)=>{
     // *********************************
    // The aggregation does not return anything:
     var aggregation_cursor=db.getCollection("collection_main").aggregate(
    [
        { 
            "$match" : {
                $and:[
                {"nation" : "AUS"},{
                item : {"$exists" : true}
                }]
            }
        },{ 
            "$group" : {
                "_id" : {
                    "year" : "$year","nation" : "$nation"
                },"value" : {
                    "$sum" : "$item"
                }
            }
        },{ 
            "$project" : {
                "_id" : NumberInt(0),"year" : "$_id.year","nation" : "$_id.nation","value" : "$value","description.name": "$item"
            }
        }
    ]
);

while (aggregation_cursor.hasNext()) {
 print(tojson(aggregation_cursor.next()));
}
     // *********************************
     
   })
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)