[SERVER-12661] Unable to determine if field is of type array when field name is all numeric characters Created: 09/Feb/14  Updated: 10/Dec/14  Resolved: 09/Feb/14

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

Type: Question Priority: Major - P3
Reporter: Brad Miller Assignee: Unassigned
Resolution: Done Votes: 0
Labels: query
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

BSON allows for a field name to be all numeric characters. For example:

{ "_id" : 102, "0" : [ "Junk", "Garbage", "ETC" ], "name" :

{ "first" : "John", "last" : "Backus" }

, "birth" : ISODate("1924-12-03T05:00:00Z"), "death" : ISODate("2007-03-17T04:00:00Z"), "contribs" : [ "Fortran", "ALGOL", "Backus-Naur Form", "FP" ], "awards" : [

{ "award" : "W.W. McDowell Award", "year" : 1967, "by" : "IEEE Computer Society" }

,

{ "award" : "National Medal of Science", "year" : 1975, "by" : "National Science Foundation" }

,

{ "award" : "Turing Award", "year" : 1977, "by" : "ACM" }

,

{ "award" : "Draper Prize", "year" : 1993, "by" : "National Academy of Engineering" }

] }

I am currently working on a system where I need to evaluate every field in a mongo document at every level of the document. One of the pieces of data I collect about each field is whether or not the field is an array.

In my simple example above, If I want to determine that field "0" is an array, I execute the following:

db.people.find({$where : "Array.isArray(this.0)"})

This results in an error:
error:

{ "$err" : "SyntaxError: Unexpected number", "code" : 16722 }

I've scoured the internet trying to find an alternative way to get this information, but I've not been successful. Is there another way to determine whether a field is an array in scenario where the field name all numeric characters?

Thanks



 Comments   
Comment by Brad Miller [ 09/Feb/14 ]

Excellent....works perfectly. Thank you so much for the quick reply.

Comment by Asya Kamsky [ 09/Feb/14 ]

Also it works if you make 0 quoted via

db.people.find({$where : "Array.isArray(this['0'])"})

Comment by Asya Kamsky [ 09/Feb/14 ]

You can use:

db.people.find({"0.0":{$exists:true}}).count()
1
db.people.find({"awards.0":{$exists:true}}).count()
1
db.people.find({"name.0":{$exists:true}}).count()
0

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