1
var foo = [ [ 14, 31, 55, 56, 60, 19 ], [30, 32, 33, 50, 64, 6 ], [9, 15, 22, 35, 48, 3] ];

var bar = await Model.find({
    numbers: { $in: foo }
  });

console.log(bar);

When I try to run the above code above I get the error below. The model is a mongoose model and the query runs with no problems in a raw mongodb query using robomongo.

{ [CastError: Cast to number failed for value "14,31,55,56,60,19" at path "numbers"]
  message: 'Cast to number failed for value "14,31,55,56,60,19" at path "numbers"',
  name: 'CastError',
  kind: 'number',
  value: [ 14, 31, 55, 56, 60, 19 ],
  path: 'numbers',
  reason: undefined }
3
  • 1
    Can you show how you have set up the model? Is the field numbers defined to be an array of the type Number? If so, it would fail. Commented Apr 14, 2016 at 20:00
  • The CastError is simply because your schema is defined as Number when you are in fact trying to compare "Arrays" being an array of numbers inside the $in. If you are saying this works without a schema involved the n perhaps your schema is incorrect for the data stored, since the "numbers" values themselves would be "arrays" rather than a single value defined in the schema. Your schema type should therefore be "numbers": [Number] and not "numbers": Number as you likely have them defined. Commented Apr 14, 2016 at 23:38
  • 1
    Note also that exact matching is probably not what you "really" want, and instead should be probably using a series of $all operations inside an $or expression. In that way the "sequence" of the numbers in the arrays does not need to be an "exact match" for the array as presented, but instead only needs to contain "all" the values in the list. Commented Apr 14, 2016 at 23:40

1 Answer 1

1

You're passing foo, which is an array of arrays to Mongoose with a property which is expecting a Number value, thus Mongoose is complaining to you when it does it's validation by saying that an array is not a number.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.