Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object type in mongoose

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 ?

like image 277
codewarrior Avatar asked Feb 03 '17 08:02

codewarrior


People also ask

What is the type of Mongoose object ID?

Mongoose casts 24 char strings to ObjectIds for you based on your schema paths. There are several other values that Mongoose can cast to ObjectIds. The key lesson is that an ObjectId is 12 arbitrary bytes. Any 12 byte buffer or 12 character string is a valid ObjectId.

What is schema in Mongoose?

A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.

What is select in Mongoose schema?

select() is a method of Mongoose that is used to select document fields that are to be returned in the query result. It is used to include or exclude document fields that are returned from a Mongoose query. The select() method performs what is called query projection.

What is __ V in MongoDB?

The __v field is called the version key. It describes the internal revision of a document. This __v field is used to track the revisions of a document. By default, its value is zero.


1 Answers

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() 
like image 66
Justin Avatar answered Sep 21 '22 09:09

Justin