62

I am defining a mongoose schema and definition is as follows:

   inventoryDetails: {
        type: Object,
        required: true

    },
    isActive:{
        type:Boolean,
        default:false
    }

I tried "Object" type and I am seeing my data is getting saved successfully. When I changed type to array, the save is failing.

Sample Data:

{
    "inventoryDetails" : { 
        "config" : { 
            "count" : { 
                "static" : { "value" : "123" }, 
                "dataSource" : "STATIC" 
            }, 
            "title" : { 
                "static" : { "value" : "tik" }, 
                "dataSource" : "STATIC" 
            } 
        }, 
        "type" : "s-card-with-title-count" 
    } 
}

"Object" type is not one of the types that mongoose allows. But, how it is being supported ?

chridam
  • 100,957
  • 23
  • 236
  • 235
codewarrior
  • 813
  • 1
  • 6
  • 10
  • What exactly fails for you? Did you check out the documentation. Object type is supported. Array is supported as well. http://mongoosejs.com/docs/schematypes.html – Antonio Narkevich Feb 03 '17 at 09:29
  • 8
    yes, I went through the documentation and the following are supported. String,Number,Date,Buffer,Boolean,Mixed,Objectid,Array. Where is Object mentioned? And nothing is failing. Just wanted to know whether mongoose implicitly supports javascript datatypes – codewarrior Feb 03 '17 at 11:20

3 Answers3

123

You have two options to get your Object in the db:

1. Define it by yourself

let YourSchema = new Schema({
  inventoryDetails: {
    config: {
      count: {
        static: {
          value: {
            type: Number,
            default: 0
          },
          dataSource: {
            type: String
          }
        }
      }
    },
    myType: {
      type: String
    }
  },
  title: {
    static: {
      value: {
        type: Number,
        default: 0
      },
      dataSource: {
        type: String
      }
    }
  }
})

Take a look at my real code:

let UserSchema = new Schema({
  //...
  statuses: {
    online: {
      type: Boolean,
      default: true
    },
    verified: {
      type: Boolean,
      default: false
    },
    banned: {
      type: Boolean,
      default: false
    }
  },
  //...
})

This option gives you the ability to define the object's data structure.

If you want a flexible object data structure, see the next one.

2. Use the default Schema.Types.Mixed type

Example taken from the doc:

let YourSchema = new Schema({
  inventoryDetails: Schema.Types.Mixed
})

let yourSchema = new YourSchema;

yourSchema.inventoryDetails = { any: { thing: 'you want' } }

yourSchema.save()
Justin
  • 4,400
  • 2
  • 32
  • 36
7

Mongoose 5

An "anything goes" SchemaType. Mongoose will not do any casting on mixed paths. You can define a mixed path using Schema.Types.Mixed or by passing an empty object literal. The following are equivalent.

const Any = new Schema({ any: {} });
const Any = new Schema({ any: Object });
const Any = new Schema({ any: Schema.Types.Mixed });
const Any = new Schema({ any: mongoose.Mixed });

For your current schema you can use the next example:

import mongoose from 'mongoose'

const inventorySchema = mongoose.Schema({
  inventoryDetails: {
    type: mongoose.SchemaTypes.Mixed,
    required: true
  },
  isActive:{
    type: Boolean,
    default:false
  }
})

const inventory = await inventorySchema.create({
  inventoryDetails: { 
    config: { 
      count: { 
        static: { value: '123' }, 
        dataSource: 'STATIC' 
      }, 
      title: { 
        static: { value: 'tik' }, 
        dataSource: 'STATIC' 
      } 
    }, 
    type: 's-card-with-title-count' 
  }
})

console.log(inventory)
Andres Separ
  • 3,144
  • 1
  • 19
  • 17
0

From the mongoose docs: https://mongoosejs.com/docs/schematypes.html#what-is-a-schematype

...// anything: Schema.Types.Mixed //...

Mixed is an "anything goes" schema type. The following are equivalent.

const Any = new Schema({ any: {} });
const Any = new Schema({ any: Object });
const Any = new Schema({ any: Schema.Types.Mixed });
const Any = new Schema({ any: mongoose.Mixed });

So, I think that's why setting type:Object worked.