[SERVER-69239] $lastN ordering issue Created: 30/Aug/22  Updated: 30/Aug/22  Resolved: 30/Aug/22

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 6.0.1
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Justin Lee Assignee: Yuan Fang
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to DOCS-15592 [Server] Error in $lastN (aggregation... Closed
Operating System: ALL
Steps To Reproduce:

Simply click the copy buttons for the data and the pipeline and see the results.

Participants:

 Description   

If I run the example here:  https://www.mongodb.com/docs/manual/reference/operator/aggregation/lastN/#computing-n-based-on-the-group-key-for–group, I get the following results:

{ "_id" : \{ "gameId" : "G1" }

, "gamescores" : [ 33, 99, 1 ] }

{ "_id" : \{ "gameId" : "G2" }

, "gamescores" : [ 80 ] }

But the docs show, correctly, that for G2 there should be only a 10 there.  I'm literally using the 'copy' action on the page and pasting in to my local shell and getting those results.  Am I tripping or is there a bug here? 

server build info as follows:

db.serverBuildInfo()
{
        "version" : "6.0.1",
        "gitVersion" : "32f0f9c88dc44a2c8073a5bd47cf779d4bfdee6b",
        "targetMinOS" : "Windows 7/Windows Server 2008 R2",
        "modules" : [ ],
        "allocator" : "tcmalloc",
        "javascriptEngine" : "mozjs",
        "sysInfo" : "deprecated",
        "versionArray" : [
                6,
                0,
                1,
                0
        ],
        "openssl" :

{                 "running" : "Windows SChannel"         }

,
        "buildEnvironment" :

{                 "distmod" : "windows",                 "distarch" : "x86_64",                 "cc" : "cl: Microsoft (R) C/C++ Optimizing Compiler Version 19.31.31107 for x64",                 "ccflags" : "/nologo /WX /FImongo/platform/basic.h /EHsc /W3 /wd4068 /wd4244 /wd4267 /wd4290 /wd4351 /wd4355 /wd4373 /wd4800 /wd4251 /wd4291 /we4013 /we4099 /we4930 /errorReport:none /MD /O2 /Oy- /bigobj /utf-8 /permissive- /Zc:__cplusplus /Zc:sizedDealloc /volatile:iso /diagnostics:caret /std:c++17 /Gw /Gy /Zc:inline",                 "cxx" : "cl: Microsoft (R) C/C++ Optimizing Compiler Version 19.31.31107 for x64",                 "cxxflags" : "/TP",                 "linkflags" : "/nologo /DEBUG /INCREMENTAL:NO /LARGEADDRESSAWARE /OPT:REF",                 "target_arch" : "x86_64",                 "target_os" : "windows",                 "cppdefines" : "SAFEINT_USE_INTRINSICS 0 PCRE_STATIC NDEBUG BOOST_ALL_NO_LIB _UNICODE UNICODE _SILENCE_CXX17_ALLOCATOR_VOID_DEPRECATION_WARNING _SILENCE_CXX17_OLD_ALLOCATOR_MEMBERS_DEPRECATION_WARNING _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING _SILENCE_ALL_CXX20_DEPRECATION_WARNINGS _CONSOLE _CRT_SECURE_NO_WARNINGS _ENABLE_EXTENDED_ALIGNED_STORAGE _SCL_SECURE_NO_WARNINGS _WIN32_WINNT 0x0A00 BOOST_USE_WINAPI_VERSION 0x0A00 NTDDI_VERSION 0x0A000000 BOOST_THREAD_VERSION 5 BOOST_THREAD_USES_DATETIME BOOST_SYSTEM_NO_DEPRECATED BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS BOOST_ENABLE_ASSERT_DEBUG_HANDLER BOOST_LOG_NO_SHORTHAND_NAMES BOOST_LOG_USE_NATIVE_SYSLOG BOOST_LOG_WITHOUT_THREAD_ATTR ABSL_FORCE_ALIGNED_ACCESS"         }

,
        "bits" : 64,
        "debug" : false,
        "maxBsonObjectSize" : 16777216,
        "storageEngines" : [
                "devnull",
                "ephemeralForTest",
                "wiredTiger"
        ],
        "ok" : 1
}



 Comments   
Comment by Justin Lee [ 30/Aug/22 ]

Fabulous.  I was curious if that was the case.  I'll update bits on my end, too.  Thank you for confirming.

Comment by Yuan Fang [ 30/Aug/22 ]

Hi jlee@antwerkz.com,

Thank you for reporting this. After an investigation, I found an error in the document you referred to:

The result of Computing n Based on the Group Key for $group (without using $sort in descending order) that is shown in the document:

[
   { _id: { gameId: "G2" }, gamescores: [ 10 ] },
   { _id: { gameId: "G1" }, gamescores: [ 33, 31, 1 ] }
]

should be corrected to:

[
   { _id: { gameId: "G2" }, gamescores: [ 80 ] },
   { _id: { gameId: "G1" }, gamescores: [ 33, 31, 1 ] }
]

The results you got are actually correct. I've created a Jira ticket DOCS-15592 in the MongoDB Docs team projects. 

Regards,

Yuan

 

Generated at Thu Feb 08 06:12:57 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.