-
Type: New Feature
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: 1.6.2
-
Component/s: Querying
-
Environment:Mac OSX 64bit
-
Query Optimization
It seems reasonable that if dot-notation works as a field-selector when selecting object members, that it should also work when selecting array indexes,
but that doesn't seem to be the case with the current version of MongoDB.
Case:
// Get the entire array (succeeds): > db.foo.find({"key":"12345"}, {"array_field"}) { "_id" : ObjectId("4c9a41204cb16aa4fe28c4cc"), "array_field" : [ { "bar" : "data", "bam" : "more data", } ] } // Attempt to get first element in array (index 0): > db.foo.find({"key":"12345"}, {"array_field.0":1}) { "_id" : ObjectId("4c9a41204cb16aa4fe28c4cc"), "array_field" : [ { } ] }
The second command fails to return anything. You can in fact use the $slice operator to get the element, but then the command becomes large and ugly:
> db.foo.find({"key":"12345"}, {_id:1, array_field:{$slice:[0,1]}})
When developing custom ORM classes to access Mongo, this special case will be needed when accessing an array element by index. This seems unnecessary.
I suggest that Mongo handle dot-notation the same for Arrays as it already does for Objects, and simply allow you to select by index. (Note, that a find query does allow array indexes in the dot notation, but the field select param does not)
- is duplicated by
-
SERVER-5299 array element selection syntax returns wrong result
- Closed
- is related to
-
SERVER-50504 $facet+$match on positional path doesn't seem to pass results to certain subsequent stages
- Closed