社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Buzz Moschetti

Buzz Moschetti 最近创建的主题
Buzz Moschetti 最近回复了
7 年前
回复了 Buzz Moschetti 创建的主题 » mongodb query and projection on subdocument数组还返回另一个文档的数组

这不是用 mongoose 但这将在一个或多个文档中的站点数组中找到一个特定的站点名称,并且只返回 measures 数组:

db.foo.aggregate([
// First, find the docs we are looking for:
{$match: {"stations.stationName": "livingroom"}}

// Got the doc; now need to fish out ONLY the desired station.  The filter will
// will return an array so use arrayElemAt 0 to extract the object at offset 0.
// Call this intermediate qqq:
,{$project: { qqq:
      {$arrayElemAt: [
          { $filter: {
            input: "$stations",
            as: "z",
            cond: { $eq: [ "$$z.stationName", "livingroom" ] }
      }}, 0]
      } 
}}

// Lastly, just project measures and not _id from this object:
,{$project: { _id:0, measures: "$qqq.measures" }}
                  ]);