Skip to content

Why do Array and mongoose.Schema.Types.Array behave differently? #9194

Closed
@jgraydus

Description

@jgraydus

I've encountered surprising behavior with schemas that use mongoose.Schema.Types.Array. The following is a minimal example to demonstrate.

const mongoose = require('mongoose')

const Model = mongoose.model('Foo', {
  array1: Array,
  array2: mongoose.Schema.Types.Array
})

const item = new Model({
  array1: [1,2,3],
  array2: [1,2,3]
})

console.log(mongoose.version)
console.log(item)

The output is

5.9.21
{
  array1: [ 1, 2, 3 ],
  array2: [ [ 1 ], [ 2 ], [ 3 ] ],
  _id: 5efe4b84eae249003167a757
}

Notice that array2, which is specified as a mongoose.Schema.Types.Array, becomes an array of arrays.

Is this the expected behavior? If so, where is this documented?

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions