Запрос, чтобы получить последний просмотренный эпизод и игнорировать любые другие дубликаты/старые данные

#node.js #mongodb #mongodb-query #aggregation-framework

Вопрос:

Мои данные выглядят так:

 [{
"_id": {
    "$oid": "607d7283bb6e270009ec9eab"
},
"anime_id": "644079",
"episode_id": "2498309",
"episode_number": "3",
"slug": "black-clover",
"username": "test",
"__v": 0,
"date_session_started": {
    "$date": "2021-04-19T12:07:31.500Z"
},
"date_session_updated": {
    "$date": "2021-04-19T12:07:31.499Z"
},
"playhead": 7},

{
    "_id": {
        "$oid": "607d724fbb6e270009ec9ea7"
    },
    "anime_id": "644079",
    "episode_id": "2498311",
    "episode_number": "2",
    "slug": "black-clover",
    "username": "test",
    "__v": 0,
    "date_session_started": {
        "$date": "2021-04-19T12:06:39.668Z"
    },
    "date_session_updated": {
        "$date": "2021-04-19T12:06:53.692Z"
    },
    "playhead": 1422
},
{
    "_id": {
        "$oid": "607d12f388831b0009c926d9"
    },
    "anime_id": "1211265",
    "episode_id": "7436533",
    "episode_number": "16",
    "slug": "jujutsu-kaisen",
    "username": "test",
    "__v": 0,
    "date_session_started": {
        "$date": "2021-04-19T05:19:47.785Z"
    },
    "date_session_updated": {
        "$date": "2021-04-19T05:42:28.662Z"
    },
    "playhead": 1434
},
{
    "_id": {
        "$oid": "607d0da58df4af0009aba081"
    },
    "anime_id": "1211265",
    "episode_id": "7436531",
    "episode_number": "15",
    "slug": "jujutsu-kaisen",
    "username": "test",
    "__v": 0,
    "date_session_started": {
        "$date": "2021-04-19T04:57:09.411Z"
    },
    "date_session_updated": {
        "$date": "2021-04-19T05:19:06.746Z"
    },
    "playhead": 1434
}]
 

То, что я хочу получить, — это последний эпизод, который был просмотрен или просматривается, и игнорируйте другие эпизоды той же серии:

 [{
"_id": {
    "$oid": "607d7283bb6e270009ec9eab"
},
"anime_id": "644079",
"episode_id": "2498309",
"episode_number": "3",
"slug": "black-clover",
"username": "test",
"__v": 0,
"date_session_started": {
    "$date": "2021-04-19T12:07:31.500Z"
},
"date_session_updated": {
    "$date": "2021-04-19T12:07:31.499Z"
},
"playhead": 7},
{
    "_id": {
        "$oid": "607d12f388831b0009c926d9"
    },
    "anime_id": "1211265",
    "episode_id": "7436533",
    "episode_number": "16",
    "slug": "jujutsu-kaisen",
    "username": "test",
    "__v": 0,
    "date_session_started": {
        "$date": "2021-04-19T05:19:47.785Z"
    },
    "date_session_updated": {
        "$date": "2021-04-19T05:42:28.662Z"
    },
    "playhead": 1434
}]
 

Я попытался запросить по {date_session_updated: -1}, это позволило получить последние обновленные эпизоды, но у него все еще были другие эпизоды. Запрос, который я хотел, состоял в том, чтобы отфильтровать его до самого высокого эпизода и последнего обновления, аналогично тому, как Netflix продолжил просмотр последнего эпизода, который вы смотрели.

ИЗМЕНИТЬ: Вот весь набор данных:

     [{
    "_id": {
      "$oid": "607a7accf01dab0009abad06"
    },
    "username": "test",
    "playhead": 3,
    "anime_id": "38636",
    "slug": "one-piece",
    "episode_id": "896472",
    "episode_number": "1",
    "date_session_started": {
      "$date": "2021-04-17T06:06:04.863Z"
    },
    "__v": 0,
    "date_session_updated": {
      "$date": "2021-04-18T03:26:42.928Z"
    }
  },
  {
    "_id": {
      "$oid": "607a9a005afd820007f7f933"
    },
    "anime_id": "38636",
    "episode_id": "896477",
    "episode_number": "2",
    "slug": "one-piece",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-17T08:19:12.422Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T05:47:22.010Z"
    },
    "playhead": 96
  },
  {
    "_id": {
      "$oid": "607a9f10b0d05600095c9fad"
    },
    "anime_id": "41422",
    "episode_id": "903592",
    "episode_number": "1",
    "slug": "meitantei-conan",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-17T08:40:48.566Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T05:53:23.220Z"
    },
    "playhead": 1481
  },
  {
    "_id": {
      "$oid": "607aa28ed2490700065f1a25"
    },
    "anime_id": "961039",
    "episode_id": "5309947",
    "episode_number": "1",
    "slug": "dorohedoro",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-17T08:55:42.033Z"
    },
    "date_session_updated": {
      "$date": "2021-04-17T10:01:37.170Z"
    },
    "playhead": 2
  },
  {
    "_id": {
      "$oid": "607b995157c4290009e1e0ca"
    },
    "anime_id": "38636",
    "episode_id": "896475",
    "episode_number": "3",
    "slug": "one-piece",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T02:28:33.340Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T03:40:05.392Z"
    },
    "playhead": 3
  },
  {
    "_id": {
      "$oid": "607b9a945691fd0007d937d9"
    },
    "anime_id": "38636",
    "episode_id": "896467",
    "episode_number": "4",
    "slug": "one-piece",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T02:33:56.031Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T02:34:01.899Z"
    },
    "playhead": 2
  },
  {
    "_id": {
      "$oid": "607ba8fd5691fd0007d93809"
    },
    "anime_id": "38636",
    "episode_id": "2252943",
    "episode_number": "787",
    "slug": "one-piece",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T03:35:25.745Z"
    },
    "date_session_updated": {
      "$date": "2021-04-19T01:10:06.915Z"
    },
    "playhead": 777
  },
  {
    "_id": {
      "$oid": "607bb17c37a46e0009ee6a18"
    },
    "anime_id": "38636",
    "episode_id": "896468",
    "episode_number": "5",
    "slug": "one-piece",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T04:11:40.709Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T04:27:58.786Z"
    },
    "playhead": 1
  },
  {
    "_id": {
      "$oid": "607bba3363bc640009e5601f"
    },
    "anime_id": "1211265",
    "episode_id": "6968715",
    "episode_number": "3",
    "slug": "jujutsu-kaisen",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T04:48:51.808Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T04:54:17.015Z"
    },
    "playhead": 1330
  },
  {
    "_id": {
      "$oid": "607bba4763bc640009e56021"
    },
    "anime_id": "1211265",
    "episode_id": "6968713",
    "episode_number": "2",
    "slug": "jujutsu-kaisen",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T04:49:11.063Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T04:49:11.062Z"
    },
    "playhead": 1377
  },
  {
    "_id": {
      "$oid": "607bba5763bc640009e56023"
    },
    "anime_id": "1211265",
    "episode_id": "6910581",
    "episode_number": "1",
    "slug": "jujutsu-kaisen",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T04:49:27.320Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T04:49:27.320Z"
    },
    "playhead": 1397
  },
  {
    "_id": {
      "$oid": "607bbaed63bc640009e56028"
    },
    "anime_id": "1211265",
    "episode_id": "6968717",
    "episode_number": "4",
    "slug": "jujutsu-kaisen",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T04:51:57.708Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T09:33:37.688Z"
    },
    "playhead": 1281
  },
  {
    "_id": {
      "$oid": "607bbdf763bc640009e5603c"
    },
    "anime_id": "39183",
    "episode_id": "908018",
    "episode_number": "1",
    "slug": "yuu-yuu-hakusho",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T05:04:55.627Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T05:04:55.627Z"
    },
    "playhead": 117
  },
  {
    "_id": {
      "$oid": "607bbe9263bc640009e56045"
    },
    "anime_id": "39508",
    "episode_id": "914390",
    "episode_number": "2",
    "slug": "naruto",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T05:07:30.236Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T05:07:30.236Z"
    },
    "playhead": 0
  },
  {
    "_id": {
      "$oid": "607bbec963bc640009e56048"
    },
    "anime_id": "39508",
    "episode_id": "914339",
    "episode_number": "1",
    "slug": "naruto",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T05:08:25.003Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T05:08:25.002Z"
    },
    "playhead": 1
  },
  {
    "_id": {
      "$oid": "607bbedb63bc640009e5604a"
    },
    "anime_id": "39508",
    "episode_id": "914524",
    "episode_number": "219",
    "slug": "naruto",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T05:08:43.374Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T05:08:43.374Z"
    },
    "playhead": 1
  },
  {
    "_id": {
      "$oid": "607bc08163bc640009e56050"
    },
    "anime_id": "620039",
    "episode_id": "3281669",
    "episode_number": "1",
    "slug": "one-punch-man",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T05:15:45.591Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T05:15:56.546Z"
    },
    "playhead": 7
  },
  {
    "_id": {
      "$oid": "607bc0a563bc640009e56054"
    },
    "anime_id": "478386",
    "episode_id": "1595002",
    "episode_number": "1",
    "slug": "one-punch-man",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T05:16:21.099Z"
    },
    "date_session_updated": {
      "$date": "2021-04-19T11:59:58.208Z"
    },
    "playhead": 1383
  },
  {
    "_id": {
      "$oid": "607bc98a63bc640009e56081"
    },
    "anime_id": "41422",
    "episode_id": "903591",
    "episode_number": "2",
    "slug": "meitantei-conan",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T05:54:18.775Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T05:54:39.090Z"
    },
    "playhead": 67
  },
  {
    "_id": {
      "$oid": "607bc9d83aa0bb00091f38c0"
    },
    "anime_id": "41422",
    "episode_id": "903727",
    "episode_number": "75",
    "slug": "meitantei-conan",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T05:55:36.291Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T05:55:36.393Z"
    },
    "playhead": 1467
  },
  {
    "_id": {
      "$oid": "607bca23be4d0b0008a0e7ed"
    },
    "anime_id": "41422",
    "episode_id": "903724",
    "episode_number": "76",
    "slug": "meitantei-conan",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T05:56:51.033Z"
    },
    "date_session_updated": {
      "$date": "2021-04-19T01:14:18.499Z"
    },
    "playhead": 165
  },
  {
    "_id": {
      "$oid": "607bde2f6b312f000975d826"
    },
    "anime_id": "1190967",
    "episode_id": "7938485",
    "episode_number": "1",
    "slug": "slime-taoshite-300-nen-shiranai-uchi-ni-level-max-ni-nattemashita",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T07:22:23.555Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T07:25:17.418Z"
    },
    "playhead": 159
  },
  {
    "_id": {
      "$oid": "607be8646b312f000975d84c"
    },
    "anime_id": "966385",
    "episode_id": "4696255",
    "episode_number": "1",
    "slug": "dr-stone",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T08:05:56.281Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T08:13:25.322Z"
    },
    "playhead": 521
  },
  {
    "_id": {
      "$oid": "607bea889e9cb3000892f71e"
    },
    "anime_id": "40190",
    "episode_id": "925148",
    "episode_number": "1",
    "slug": "death-note",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T08:15:04.569Z"
    },
    "date_session_updated": {
      "$date": "2021-04-19T10:15:27.756Z"
    },
    "playhead": 400
  },
  {
    "_id": {
      "$oid": "607bf59bcc5942000929314c"
    },
    "anime_id": "40398",
    "episode_id": "928185",
    "episode_number": "1",
    "slug": "hunter-x-hunter",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T09:02:19.685Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T09:02:19.684Z"
    },
    "playhead": 958
  },
  {
    "_id": {
      "$oid": "607bfd0a9e9cb3000892f752"
    },
    "anime_id": "1211265",
    "episode_id": "7081619",
    "episode_number": "5",
    "slug": "jujutsu-kaisen",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T09:34:02.669Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T11:10:48.526Z"
    },
    "playhead": 1431
  },
  {
    "_id": {
      "$oid": "607c07b544b4f90007220732"
    },
    "anime_id": "1211265",
    "episode_id": "7081621",
    "episode_number": "6",
    "slug": "jujutsu-kaisen",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T10:19:33.044Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T10:43:38.863Z"
    },
    "playhead": 1327
  },
  {
    "_id": {
      "$oid": "607c0e1544b4f9000722073c"
    },
    "anime_id": "1211265",
    "episode_id": "7081623",
    "episode_number": "7",
    "slug": "jujutsu-kaisen",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T10:46:45.585Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T11:07:25.018Z"
    },
    "playhead": 1420
  },
  {
    "_id": {
      "$oid": "607c130644b4f9000722074f"
    },
    "anime_id": "1211265",
    "episode_id": "7081625",
    "episode_number": "8",
    "slug": "jujutsu-kaisen",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T11:07:50.728Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T19:35:29.527Z"
    },
    "playhead": 1434
  },
  {
    "_id": {
      "$oid": "607c1f6df3cc420008ff2b7c"
    },
    "anime_id": "1211265",
    "episode_id": "7265179",
    "episode_number": "9",
    "slug": "jujutsu-kaisen",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T12:00:45.063Z"
    },
    "date_session_updated": {
      "$date": "2021-04-18T23:43:06.379Z"
    },
    "playhead": 1420
  },
  {
    "_id": {
      "$oid": "607c2033f3cc420008ff2b7e"
    },
    "anime_id": "1211265",
    "episode_id": "7265185",
    "episode_number": "12",
    "slug": "jujutsu-kaisen",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T12:04:03.794Z"
    },
    "date_session_updated": {
      "$date": "2021-04-19T04:03:25.054Z"
    },
    "playhead": 1367
  },
  {
    "_id": {
      "$oid": "607cc43e6489230009680374"
    },
    "anime_id": "1211265",
    "episode_id": "7265181",
    "episode_number": "10",
    "slug": "jujutsu-kaisen",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-18T23:43:58.009Z"
    },
    "date_session_updated": {
      "$date": "2021-04-19T03:11:59.729Z"
    },
    "playhead": 1378
  },
  {
    "_id": {
      "$oid": "607cf56599fd9100094896ba"
    },
    "anime_id": "1211265",
    "episode_id": "7265183",
    "episode_number": "11",
    "slug": "jujutsu-kaisen",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-19T03:13:41.531Z"
    },
    "date_session_updated": {
      "$date": "2021-04-19T03:37:28.750Z"
    },
    "playhead": 1422
  },
  {
    "_id": {
      "$oid": "607d016f99fd9100094896f9"
    },
    "anime_id": "1211265",
    "episode_id": "7383545",
    "episode_number": "13",
    "slug": "jujutsu-kaisen",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-19T04:05:03.810Z"
    },
    "date_session_updated": {
      "$date": "2021-04-19T04:31:48.703Z"
    },
    "playhead": 1431
  },
  {
    "_id": {
      "$oid": "607d07c599fd910009489736"
    },
    "anime_id": "1211265",
    "episode_id": "7436529",
    "episode_number": "14",
    "slug": "jujutsu-kaisen",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-19T04:32:05.160Z"
    },
    "date_session_updated": {
      "$date": "2021-04-19T04:56:24.221Z"
    },
    "playhead": 1348
  },
  {
    "_id": {
      "$oid": "607d0da58df4af0009aba081"
    },
    "anime_id": "1211265",
    "episode_id": "7436531",
    "episode_number": "15",
    "slug": "jujutsu-kaisen",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-19T04:57:09.411Z"
    },
    "date_session_updated": {
      "$date": "2021-04-19T05:19:06.746Z"
    },
    "playhead": 1434
  },
  {
    "_id": {
      "$oid": "607d12f388831b0009c926d9"
    },
    "anime_id": "1211265",
    "episode_id": "7436533",
    "episode_number": "16",
    "slug": "jujutsu-kaisen",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-19T05:19:47.785Z"
    },
    "date_session_updated": {
      "$date": "2021-04-19T05:42:28.662Z"
    },
    "playhead": 1434
  },
  {
    "_id": {
      "$oid": "607d185688831b0009c926f2"
    },
    "anime_id": "1211265",
    "episode_id": "7570489",
    "episode_number": "17",
    "slug": "jujutsu-kaisen",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-19T05:42:46.531Z"
    },
    "date_session_updated": {
      "$date": "2021-04-19T06:04:33.086Z"
    },
    "playhead": 1251
  },
  {
    "_id": {
      "$oid": "607d1e6e88831b0009c92709"
    },
    "anime_id": "1211265",
    "episode_id": "7570491",
    "episode_number": "18",
    "slug": "jujutsu-kaisen",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-19T06:08:46.176Z"
    },
    "date_session_updated": {
      "$date": "2021-04-19T12:17:34.113Z"
    },
    "playhead": 253
  },
  {
    "_id": {
      "$oid": "607d6d24f4a2a70009a41665"
    },
    "anime_id": "764963",
    "episode_id": "2665233",
    "episode_number": "1",
    "slug": "douluo-dalu",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-19T11:44:36.785Z"
    },
    "date_session_updated": {
      "$date": "2021-04-19T11:44:36.774Z"
    },
    "playhead": 73
  },
  {
    "_id": {
      "$oid": "607d6f88f4a2a70009a41673"
    },
    "anime_id": "638613",
    "episode_id": "7866723",
    "episode_number": "1",
    "slug": "code-geass-fukkatsu-no-lelouch",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-19T11:54:48.805Z"
    },
    "date_session_updated": {
      "$date": "2021-04-19T11:55:18.208Z"
    },
    "playhead": 2437
  },
  {
    "_id": {
      "$oid": "607d6fd2bb6e270009ec9ea3"
    },
    "anime_id": "40079",
    "episode_id": "923562",
    "episode_number": "1",
    "slug": "akira",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-19T11:56:02.436Z"
    },
    "date_session_updated": {
      "$date": "2021-04-19T11:57:46.693Z"
    },
    "playhead": 187
  },
  {
    "_id": {
      "$oid": "607d70e3f4a2a70009a41680"
    },
    "anime_id": "478386",
    "episode_id": "1596186",
    "episode_number": "2",
    "slug": "one-punch-man",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-19T12:00:35.854Z"
    },
    "date_session_updated": {
      "$date": "2021-04-19T12:00:35.854Z"
    },
    "playhead": 25
  },
  {
    "_id": {
      "$oid": "607d71f3f4a2a70009a41685"
    },
    "anime_id": "644079",
    "episode_id": "2498313",
    "episode_number": "1",
    "slug": "black-clover",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-19T12:05:07.898Z"
    },
    "date_session_updated": {
      "$date": "2021-04-19T12:05:07.898Z"
    },
    "playhead": 1345
  },
  {
    "_id": {
      "$oid": "607d724fbb6e270009ec9ea7"
    },
    "anime_id": "644079",
    "episode_id": "2498311",
    "episode_number": "2",
    "slug": "black-clover",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-19T12:06:39.668Z"
    },
    "date_session_updated": {
      "$date": "2021-04-19T12:06:53.692Z"
    },
    "playhead": 1422
  },
  {
    "_id": {
      "$oid": "607d7283bb6e270009ec9eab"
    },
    "anime_id": "644079",
    "episode_id": "2498309",
    "episode_number": "3",
    "slug": "black-clover",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-19T12:07:31.500Z"
    },
    "date_session_updated": {
      "$date": "2021-04-19T12:07:31.499Z"
    },
    "playhead": 7
  },
  {
    "_id": {
      "$oid": "607d774bbb6e270009ec9ebb"
    },
    "anime_id": "41422",
    "episode_id": "903590",
    "episode_number": "3",
    "slug": "meitantei-conan",
    "username": "test",
    "__v": 0,
    "date_session_started": {
      "$date": "2021-04-19T12:27:55.530Z"
    },
    "date_session_updated": {
      "$date": "2021-04-19T12:30:51.754Z"
    },
    "playhead": 774
  }
]
 

Ответ №1:

Демо — https://mongoplayground.net/p/w8yiPwoIe7p

 db.collection.aggregate([
  { $sort: { "date_session_updated": -1 } },
  { $group: { _id: "$slug", doc: { $first: "$ROOT" } } },
  { $sort: { "doc.date_session_updated": -1 } }
])
 

Демо — https://mongoplayground.net/p/tCfn2EItq1J

Сортировать по "date_session_updated": -1 .

Сгруппируйте записи по "$anime_id" и возьмите first документ.

 db.collection.aggregate([
  { $sort: { "date_session_updated": -1 } },
  { $group: { _id: "$anime_id", doc: { $first: "$ROOT" } } }
])
 

Избавьтесь от doc ключа и установите его в корневой режим с помощью $replaceRoot

Демо — https://mongoplayground.net/p/_GECJ_ynIHv

 { "$replaceRoot": { "newRoot": "$doc" } }
 

Комментарии:

1. Он не возвращает максимальное значение episode_number, у него все еще есть дубликаты в результате запроса, поэтому, если есть серии с одинаковым идентификатором anime_id amp; slug, он должен возвращать только один с наибольшим номером episode_number

2. @MrGambino Какую комбинацию можно использовать для определения уникальности ?

3. Эпизод будет иметь один и тот же идентификатор anime_id amp; slug, но идентификатор эпизода уникален, и при выполнении запроса правильным результатом должен быть самый высокий номер эпизода. Например, если в базе данных есть «эпизод 2 с черным клевером» и «эпизод 3 с черным клевером», она должна возвращать «эпизод 3 с черным клевером» и не включать «эпизод 2 с черным клевером» в результат.

4. Можете ли вы добавить примеры записей, в которых вы получаете дубликаты ?

5. Я добавил весь набор данных