[SERVER-29739] Cursor array access does not work reliably Created: 20/Jun/17  Updated: 29/Jan/18  Resolved: 17/Aug/17

Status: Closed
Project: Core Server
Component/s: JavaScript, Shell
Affects Version/s: 3.3.5, 3.4.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Martin Altmayer Assignee: Mark Benvenuto
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File find_items_without_reviews.js     File inconsistent.js     File prepare.js    
Issue Links:
Duplicate
duplicates SERVER-30362 JavaScript DBCollection.toString() st... Closed
Related
related to SERVER-30362 JavaScript DBCollection.toString() st... Closed
related to SERVER-23358 Upgrade SpiderMonkey to 45.0.2 ESR Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Steps To Reproduce:
  • Execute prepare.js

for (i=1; i<=2500; i++) {
    db.items.insert({_id: i});
    if (i % 2) {
        db.reviews.insert({ item_id: i });
    }
}

Note that a review is created for every odd item.

  • Execute find_items_without_reviews.js. This script should return a list with all even numbers from 2 to 2500 because exactly the even items do not have an associated review. However, the actual result is [2, 4, 6, ..., 1998, 2000, 2001, 2002, 2003, ..., 2499, 2500], i.e. starting with 2001 also all odd numbers are returned.
Sprint: Platforms 2017-08-21
Participants:

 Description   

The MongoDB shell supports accessing a cursor's elements via array access, without first converting the cursor into an array: db.items.find(...)[0]
However, this might return 'undefined' even when the cursor actually contains elements.
I attached a script that demonstrates the issue.

Some notes:

  • The problem disappears when using the toArray-function.
  • Direct array access on a cursor seems to be not documented. However, because it is so intuitive and works in most cases, it is probably often used.
  • When running into the issue, the shell seems to be in an inconsistent state. As the 'inconsistent.js' script demonstrates, an if-clause with identical 'then' and 'else' branches might return different results depending on the condition.


 Comments   
Comment by Kelsey Schubert [ 20/Jun/17 ]

Hi martinaltmayer,

Thanks for reporting this issue, and providing very clear reproduction steps. This change of behavior appears to be result of an upgrade to SpiderMonkey, SERVER-23358. We'll continue to update this ticket as we investigate.

Kind regards,
Thomas

Generated at Thu Feb 08 04:21:42 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.