[SERVER-14330] Applying Array.isArray to a field containing a hyphen Created: 21/Jun/14  Updated: 10/Dec/14  Resolved: 08/Jul/14

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

Type: Question Priority: Major - P3
Reporter: Brad Miller Assignee: Kamran K.
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

I have a program which inspects every field in a document to see if it an array.

This works fine for:

db.bios.find({$where: "Array.isArray(this.awards)"})
<Returns values>

But not for

db.messages.find({$where: "Array.isArray(this.X-cc)"})
error:

{ "$err" : "ReferenceError: cc is not defined", "code" : 16722 }

If I single-quote the parameter value in isArray it breaks the working use case:

db.bios.find({$where: "Array.isArray('this.awards')"})
<No rows return>

and seems to fix the broken use case:
db.messages.find({$where: "Array.isArray(this.X-cc)"})
<No rows returned instead of an error>

What I'm looking for is a generic solution that can be used in both scenarios.

Thanks,

Brad



 Comments   
Comment by Kamran K. [ 18/Jul/14 ]

Glad to hear it worked!

Comment by Brad Miller [ 17/Jul/14 ]

Hi Kamran -

Apologies for my delay in getting back to you. Your suggestion worked perfectly.

Thank you so much for your help.

Brad

Comment by Kamran K. [ 02/Jul/14 ]

Hi Brad,

Have you had a chance to try the bracket notation suggestion?

Comment by Kamran K. [ 21/Jun/14 ]

Hi Brad,

'X-cc' is not a valid JavaScript identifier, so you can't use dot notation to access that property. Instead, you should use bracket notation to avoid identifier restrictions with property names: Array.isArray(this['X-cc'])

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors has a nice overview of bracket notation and dot notation for further reference.

Hope that helps!

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