本文介绍了如何在密钥上应用正则表达式来投影/过滤嵌入式密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何返回指定的嵌入式密钥吗?

I wonder know how to return the specified embedded keys ?

通过类似的查询

db.collection.find({"table_name": "change_in_real_gdp", {"data.*.actual":1, "data.*. upper_end_of_range"}})

原始文件

{
  "_id": "2012-06-20_change_in_real_gdp",
  "table_name": "change_in_real_gdp",
  "data": {
    "2007": {
      "actual": "4.8",
      "upper_end_of_range": "-",
      "upper_end_of_central_tendency": "-",
      "lower_end_of_central_tendency": "-",
      "lower_end_of_range": "-"
    },
    "2008": {
      "actual": "6.9",
      "upper_end_of_range": "-",
      "upper_end_of_central_tendency": "-",
      "lower_end_of_central_tendency": "-",
      "lower_end_of_range": "-"
    }
    }
}

预期结果

{
  "_id": "2012-06-20_change_in_real_gdp",
  "table_name": "change_in_real_gdp",
  "data": {
    "2007": {
      "actual": "4.8",
      "upper_end_of_range": "-",
    },
    "2008": {
      "actual": "6.9",
      "upper_end_of_range": "-",
    }
    }
}

推荐答案

不能在投影中使用正则表达式.您需要为 .find() 方法

You cannot use regex in projection. You need to explicitly specify the projection argument to the .find() method

db.collection.find({}, 
    { 
        "data.2007.actual": 1, 
        "data.2007.upper_end_of_range": 1, 
        "data.2008.actual": 1, 
        "data.2008.upper_end_of_range": 1, 
        "table_name": 1 
    }
)

这篇关于如何在密钥上应用正则表达式来投影/过滤嵌入式密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 19:26