it-source

mongoose의 개체

criticalcode 2023. 5. 26. 21:09
반응형

mongoose의 개체

나는 몽구스 스키마를 정의하고 있으며 정의는 다음과 같습니다.

   inventoryDetails: {
        type: Object,
        required: true

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

"Object" 유형을 시도했는데 데이터가 성공적으로 저장되고 있습니다.유형을 배열로 변경했을 때 저장이 실패했습니다.

표본 데이터:

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

"객체" 유형은 몽구스가 허용하는 유형 중 하나가 아닙니다.하지만, 그것은 어떻게 지원되고 있습니까?

두 가지 옵션이 있습니다.Objectdb:

직접 정의

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

내 실제 코드를 보십시오.

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

이 옵션을 사용하면 개체의 데이터 구조를 정의할 수 있습니다.

유연한 개체 데이터 구조를 원하는 경우 다음을 참조하십시오.

기본값 사용Schema.Types.Mixed유형

문서에서 가져온 예:

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

let yourSchema = new YourSchema;

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

yourSchema.save()

몽구스 5

"무엇이든 가능한" SchemaType.몽구스는 혼합된 경로에서 어떤 캐스팅도 하지 않을 것입니다.스키마를 사용하여 혼합 경로를 정의할 수 있습니다.유형. 혼합되거나 빈 개체 리터럴을 전달하여 입력합니다.다음은 동등합니다.

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

현재 스키마에 대해 다음 예제를 사용할 수 있습니다.

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)

몽구스 문서에서: https://mongoosejs.com/docs/schematypes.html#what-is-a-schematype

임의: 스키마.유형. 혼합 //...

혼합은 "무엇이든 가능한" 스키마 유형입니다.다음은 동등합니다.

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

그래서 유형을 설정하는 것 같습니다.개체가 작동했습니다.

언급URL : https://stackoverflow.com/questions/42019679/object-type-in-mongoose

반응형