【问题标题】:Filter out inner collection on mongodb/mongoose过滤掉 mongodb/mongoose 上的内部集合
【发布时间】:2018-11-18 18:19:54
【问题描述】:

你能告诉我如何过滤掉videoDetailsvimeo_id==null吗?根据以下查询,它检索所有数据。即包括没有 Vimeo 视频。这里我使用了 Mongoose。

数据:

{
  "_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
}

得到

 get(req) {
      mongoose.connect(config.mongoConnectionUrl, { useMongoClient: true })
      return Playlist
        .findOne({})
        .exec()
        .then((playlist) => {
          mongoose.disconnect()
          return Promise.resolve({ error: false, playlist })
        })
        .catch((e) => {
          mongoose.disconnect()
          const err = prepareApiError(e)
          return new api.ApiResponse({ error: true, reason: err.reason }, { "Content-Type": "application/json" }, err.errorCode)
        })
    },

【问题讨论】:

  • 下面的答案有问题吗?

标签: mongodb mongoose mongodb-query aggregation-framework


【解决方案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] }
            }
          }
        }
      }
    }
  }}
])

【讨论】:

  • 因为没有拿到上面的任务,所以没有测试这个。
猜你喜欢
  • 2021-12-11
  • 1970-01-01
  • 2018-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-05
  • 1970-01-01
相关资源
最近更新 更多