Mongoose query model where field type is an array

Viewed 112

I had to change the schema type of a field (location) in my Course model from array to string, I however need to change all previous data I have from type array to string and then do some data manipulation to get what I need. New data are being stored as String. How do I query all courses having location field type as array and where zip code exists so that I can do proper data manipulation?

const coursesWithArrayLocation = await Course.find({zipcode: { $exists: true }});
1 Answers

Try $type operator to match specific type of field,

  • match type by 2 ways first is Number and second is Alias, where array type Number is 4 and Alias is "array"
const coursesWithArrayLocation = await Course.find({
  location: { $type: "array" },
  zipcode: { $exists: true }
})

Playground

Related