过滤掉mongodb / mongoose上的内部集合

时间:2018-09-12 12:52:20

标签: mongodb mongoose mongodb-query aggregation-framework

您能告诉我如何在videoDetails的{​​{1}}处过滤吗?根据以下查询,它将检索所有数据。即包括无Vimeo视频。这里我用过猫鼬。

数据:

vimeo_id==null

获取

{
  "_id": ObjectId("5b10e8c475356969da5f91b2"),
  "playlists": [{
      "listId": "5b26a1040c4ebb107f0038e5",
      "title": "Featured Stories (copy)",
      "playlist_item_count": 1,
      "sortOrder": 1,
      "_id": ObjectId("5b37adf40fc31552f9280603"),
      "isPrimary": true,
      "videoDetails": [{
        "short_description": "",
        "vimeo_id": null,
        "title": "Managing-Time-Effectively-Section-4-Lecture-1-Questions-and-Answers.mp4",
        "duration": 493,
        "videoId": "5a3c2f03e338f91564000130",
        "_id": ObjectId("5b37adf40fc31552f9280604"),
        "accessLevel": "public"
      }]
    },
    {
      "listId": "5b375560d80ed51238004998",
      "title": "Atv-test",
      "playlist_item_count": 1,
      "sortOrder": 1,
      "_id": ObjectId("5b37adf40fc31552f9280601"),
      "isPrimary": false,
      "videoDetails": [{
        "short_description": "When Alice’s husband arrives home to try and make amends it leaves her navigating a tricky situation.",
        "vimeo_id": "277029143",
        "title": "Love Is Blind",
        "duration": 385,
        "videoId": "5b375add2ef7c612f1002304",
        "presentedBy": "CEO TEAM",
        "_id": ObjectId("5b37adf40fc31552f9280602"),
        "accessLevel": "public"
      }]
    }
  ],
  "__v": 90
}

1 个答案:

答案 0 :(得分:1)

您可以尝试以下汇总

db.collection.aggregate([
  { "$project": {
    "playlists": {
      "$map": {
        "input": "$playlists",
        "as": "play",
        "in": {
          "listId": "$$play.listId",
          "title": "$$play.title",
          "playlist_item_count": "$$play.playlist_item_count",
          "sortOrder": "$$play.sortOrder",
          "_id": "$$play._id",
          "isPrimary": "$$play.isPrimary",
          "videoDetails": {
            "$filter": {
              "input": "$$play.videoDetails",
              "as": "video",
              "cond": { "$ne": ["$$video.vimeo_id", null] }
            }
          }
        }
      }
    }
  }}
])