Mongoose query model where field type is an array

mykoman

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 }});
turivishal

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mongoose nested query on Model by field of its referenced model

From Dev

Mongoose : query on a field on an array of ref documents,

From Dev

Query documents with condition on array field in mongoose

From Dev

Mongoose: assign field of type 'array of Strings'

From Dev

mongoose model $in query not working

From Dev

Mongoose query with a list in the model

From Dev

Query mongoDB with mongoose model

From Dev

query builder add condition on field type array

From Dev

mongoose where query with "or"

From Dev

Mongoose Query with where

From Dev

Query for array elements inside JSON[] field array type

From Dev

Mongoose query on json array

From Dev

Mongoose query with array parameter

From Dev

Overriding mongoose query for a specific model

From Dev

Mongoose query where X is in both arrays and where Y is in only one array

From Dev

Mongoose query where X is in both arrays and where Y is in only one array

From Dev

Mongoose Query Where Clause with Wildcard

From Dev

postgresql and RoR - how quering a json type field where value is in an array

From Dev

postgresql and RoR - how quering a json type field where value is in an array

From Dev

mongoose - query on a field that exists inside of another field

From Dev

or where query on model

From Dev

Mongoose slice array, in populated field

From Dev

Mongoose - get length of array in model

From Dev

Mongoose, Deep Population on array model

From Dev

Populate an array of Model objects with mongoose

From Dev

Populate an array of Model objects with mongoose

From Dev

Mongoose use array in find().where()

From Dev

How do you do a "where in" sql query with an array data type?

From Dev

Mongoose query that finds all items of type B with entries in B.array that have a match in A.array

Related Related

  1. 1

    Mongoose nested query on Model by field of its referenced model

  2. 2

    Mongoose : query on a field on an array of ref documents,

  3. 3

    Query documents with condition on array field in mongoose

  4. 4

    Mongoose: assign field of type 'array of Strings'

  5. 5

    mongoose model $in query not working

  6. 6

    Mongoose query with a list in the model

  7. 7

    Query mongoDB with mongoose model

  8. 8

    query builder add condition on field type array

  9. 9

    mongoose where query with "or"

  10. 10

    Mongoose Query with where

  11. 11

    Query for array elements inside JSON[] field array type

  12. 12

    Mongoose query on json array

  13. 13

    Mongoose query with array parameter

  14. 14

    Overriding mongoose query for a specific model

  15. 15

    Mongoose query where X is in both arrays and where Y is in only one array

  16. 16

    Mongoose query where X is in both arrays and where Y is in only one array

  17. 17

    Mongoose Query Where Clause with Wildcard

  18. 18

    postgresql and RoR - how quering a json type field where value is in an array

  19. 19

    postgresql and RoR - how quering a json type field where value is in an array

  20. 20

    mongoose - query on a field that exists inside of another field

  21. 21

    or where query on model

  22. 22

    Mongoose slice array, in populated field

  23. 23

    Mongoose - get length of array in model

  24. 24

    Mongoose, Deep Population on array model

  25. 25

    Populate an array of Model objects with mongoose

  26. 26

    Populate an array of Model objects with mongoose

  27. 27

    Mongoose use array in find().where()

  28. 28

    How do you do a "where in" sql query with an array data type?

  29. 29

    Mongoose query that finds all items of type B with entries in B.array that have a match in A.array

HotTag

Archive