[SERVER-2598] Curious indexing behavior with arrays Created: 21/Feb/11  Updated: 06/Nov/19  Resolved: 26/Nov/12

Status: Closed
Project: Core Server
Component/s: Index Maintenance
Affects Version/s: 1.7.6
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: W. van der Laan Assignee: Unassigned
Resolution: Duplicate Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Linux Ubuntu 64 bit


Issue Links:
Depends
is depended on by SERVER-44393 indexu.js fails in replica_set_passth... Closed
Duplicate
duplicates SERVER-2902 Index keys are not generated for nume... Closed
Operating System: ALL
Participants:

 Description   

Queries can return different results depending on whether an index exists or not, when Indexing on lists.

It is best illustrated with code:

> db.test.insert(

{a:[1,1,1]}

)
> db.test.insert(

{a:[1,1,2]}

)
> db.test.insert(

{a:[1,1,3]}

)
> db.test.insert(

{a:[1,2]}

)
> db.test.insert(

{a:[1,2,1]}

)
> db.test.insert(

{a:[2,1]}

)
> db.test.insert(

{a:[3]}

)

> db.test.find(

{'a.0':1}

)

{ "_id" : ObjectId("4d60ceef276d207f24f8c3b2"), "a" : [ 1, 1, 1 ] } { "_id" : ObjectId("4d60cef1276d207f24f8c3b3"), "a" : [ 1, 1, 2 ] } { "_id" : ObjectId("4d60cef2276d207f24f8c3b4"), "a" : [ 1, 1, 3 ] } { "_id" : ObjectId("4d60cef9276d207f24f8c3b5"), "a" : [ 1, 2 ] } { "_id" : ObjectId("4d60cf04276d207f24f8c3b6"), "a" : [ 1, 2, 1 ] }

Unless you add an index:
> db.test.ensureIndex(

{'a.0':1}

)
> db.test.find(

{'a.0':1}

)

Same query now returns nothing. Returning nothing is probably what is expected in this case.



 Comments   
Comment by Chris Westin [ 13/Oct/11 ]

This is almost certainly not working because the index match is looking for a scalar valued field: {a:{"0":1}}. JavaScript is broken in that a.0 is really the same as a[0]; but a:

{"0":...}

and a:[...] aren't really the same as far as a user is concerned. We've got similar update bugs where folks try to upsert things with 'a.0' in them and end up with a:

{"0":<value>}

when they want a new array with their value as the first element. Perhaps we should just put a stack in the ground here and require the use of 'a.0' or 'a[0]' to explicitly declare the user's intent in these cases.

Generated at Thu Feb 08 03:00:30 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.