You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MongoDB未知键对象Projection配置:仅返回子对象return属性

MongoDB动态键文档的投影实现

现有如下包含未知动态键的MongoDB文档结构:

{
  data: {
    someObjectIdStringThatCantBePutInProjection: {
      dontReturn: 123,
      return: 321
    },
    someOtherObjectIdStringThatCantBePutInProjection: {
      dontReturn: 1234,
      return: 4321
    }
  }
}

需求:仅返回data字段下所有子对象的return属性。


对应的投影(Projection)可以用两种等价写法:

写法一:通配符点语法

{
  "data.*.return": 1
}

写法二:嵌套通配符结构

{
  data: {
    "*": {
      return: 1
    }
  }
}

执行上述投影后,预期返回结果为:

{
  data: {
    someObjectIdStringThatCantBePutInProjection: {
      return: 321
    },
    someOtherObjectIdStringThatCantBePutInProjection: {
      return: 4321
    }
  }
}

内容的提问来源于stack exchange,提问作者1baz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 07:22:53