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
相关产品推荐
相关产品推荐

