如何解决在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 举报,一经查实,本站将立刻删除。