The _getLastDocID() function checks the last oplog entry of each shard to figure out the maxDocID. It's not guaranteed that the last oplog entry will be an insert of a document containing the docId field:
[js_test:sharded_pit_selective_backup_restore_simple] [jsTest] ---- [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] Last docID: 167, lastEntries: [ [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] { [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "op" : "u", [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "ns" : "config.cache.databases", [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "ui" : UUID("b3fb3e73-cc56-43fa-bbce-62362eaef69e"), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "o" : { [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "$v" : 2, [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "diff" : { [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "u" : { [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "version" : { [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "uuid" : UUID("bf948bf4-90c6-46b3-9074-4b63668704ee"), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "timestamp" : Timestamp(0, 0), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "lastMod" : 0 [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] } [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] } [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] } [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] }, [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "o2" : { [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "_id" : "config" [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] }, [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "ts" : Timestamp(1654660031, 7), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "t" : NumberLong(1), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "v" : NumberLong(2), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "wall" : ISODate("2022-06-08T03:47:11.858Z") [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] }, [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] { [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "op" : "i", [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "ns" : "test.continuous_writes_unrestored", [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "ui" : UUID("ca3f3e6f-d4ab-40ec-9d2d-b8ef968d94be"), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "o" : { [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "_id" : ObjectId("62a01bbe4418e4e1315b8741"), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "shardId" : 1, [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "numForPartition" : -50, [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "docId" : 165 [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] }, [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "o2" : { [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "numForPartition" : -50, [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "_id" : ObjectId("62a01bbe4418e4e1315b8741") [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] }, [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "ts" : Timestamp(1654660030, 16), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "t" : NumberLong(1), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "v" : NumberLong(2), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "wall" : ISODate("2022-06-08T03:47:11.012Z") [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] }, [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] { [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "op" : "i", [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "ns" : "test.continuous_writes_unrestored", [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "ui" : UUID("ca3f3e6f-d4ab-40ec-9d2d-b8ef968d94be"), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "o" : { [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "_id" : ObjectId("62a01bbf4418e4e1315b8745"), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "shardId" : 2, [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "numForPartition" : 50, [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "docId" : 166 [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] }, [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "o2" : { [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "numForPartition" : 50, [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "_id" : ObjectId("62a01bbf4418e4e1315b8745") [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] }, [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "ts" : Timestamp(1654660031, 2), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "t" : NumberLong(1), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "v" : NumberLong(2), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "wall" : ISODate("2022-06-08T03:47:11.343Z") [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] }, [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] { [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "op" : "i", [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "ns" : "test.continuous_writes_unrestored", [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "ui" : UUID("ca3f3e6f-d4ab-40ec-9d2d-b8ef968d94be"), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "o" : { [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "_id" : ObjectId("62a01bbf4418e4e1315b8749"), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "shardId" : 3, [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "numForPartition" : 150, [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "docId" : 167 [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] }, [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "o2" : { [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "numForPartition" : 150, [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "_id" : ObjectId("62a01bbf4418e4e1315b8749") [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] }, [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "ts" : Timestamp(1654660031, 4), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "t" : NumberLong(1), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "v" : NumberLong(2), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "wall" : ISODate("2022-06-08T03:47:11.567Z") [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] }, [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] { [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "op" : "n", [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "ns" : "", [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "o" : { [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "msg" : "periodic noop" [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] }, [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "ts" : Timestamp(1654660031, 1), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "t" : NumberLong(1), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "v" : NumberLong(2), [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] "wall" : ISODate("2022-06-08T03:47:11.270Z") [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] } [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] ] [js_test:sharded_pit_selective_backup_restore_simple] [jsTest] ----
As seen in the code snippet above, the first element of lastEntries is not an insert of a document with the docId field. We should filter for oplog entries containing docId and look at the last one then to properly compute the maxDocID.