2016-08-21 3 views
0

현재 4 가지 모델이 있습니다. 사용자, 프로필, 관심사 및 토큰. 사용자와 프로필 사이에는 일대일 관계가 있습니다. 사용자와 토큰 사이에는 일대 다 (one to many) 관계가 있습니다. 프로필과 관심사 사이에는 일대 다 관계가 있으며 관리자가 나중에 추가 할 수있는 기능이 미리 정의됩니다.관계로 몽구스 스키마를 더 잘 구조화하는 방법

사용자

var UserSchema = new Schema({ 

    email: { 
     type: String, 
     lowercase: true, 
     unique: true, 
     required: true 
    }, 

    phone: [{countrycode: String}, {number: String}], 

    tokens: [{type: Schema.Types.ObjectId, ref: 'Token'}], 

    profile: { 
     type: Schema.Types.ObjectId, ref: 'Profile' 
    }, 
}, 
{ 
    timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'} 
}); 

프로필

var ProfileSchema = new Schema({ 

     username: { 

      type: String, 
      unique: true, 
     }, 

     firstname: { 

      type: String 
     }, 

     lastname: { 

      type: String 
     }, 

     gender: { 

      type: String 
     }, 

     dob: { 

      type: Date 
     }, 

     country: { 

      type: String 
     }, 

     city: { 

      type: String 
     }, 

     avatar: { 
      type: String 
     }, 

     about: { 
      type: String 
     }, 

     interests: [{ 
      type: Schema.Types.ObjectId, 
      ref: 'Interest' 

     }], 

    }, 
    { 
     timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'} 
    }); 

토큰

var TokenSchema = new Schema({ 

     name: { 
      type: String, 
     }, 
     value: { 
      type: String, 
     }, 
    }, 
    { 
     timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'} 
    }); 

관심

var InterestSchema = new Schema({ 

     name: { 
      type: String, 
      unique: true, 
     }, 
    }, 
    { 
     timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'} 
    }); 

이 schemeas/관계를 제대로 설정 했습니까? 이제 사용자에게 역할을 부여하려면 새 역할 스키마를 만들까요?

감사합니다.

답변

0

나는 당신이되는 NoSQL에 관계를 추가 할 수 없습니다

+0

임처럼, 다른 스키마 필드의 유형으로 스키마를 사용, 그래서 난 사람들을 저장하는 데 필요한 경우가 구성되는 방법을 말해 것입니다 데이터? – KayTokyo

0

DB를 NoSQL의에서 관계를 만들고 싶어 당신이 관계형 데이터베이스를 필요가 있다고 생각합니다. 당신이 할 수있는 유일한 방법은없는 NoSQL DB를 사용하는 방법을 학습

var Comments = new Schema({ 
    title: String, 
    body: String, 
    date: Date 
}); 

var BlogPost = new Schema({ 
    author: ObjectId, 
    title: String, 
    body: String, 
    date: Date, 
    comments: [Comments], 
    meta: { 
    votes : Number, 
    favs : Number 
    } 
}); 

mongoose.model('BlogPost', BlogPost); 

Embedded Documents

+0

블로그 게시물에 주석을 어떻게 연관 짓습니까? 그런 식으로 – KayTokyo

+0

Theres 관계 없음. 이것은 BlogPosts에서 Comment 스키마의 구조와 함께 주석 배열을 갖게되는 것을 허용합니다. 일부 BlogPost를 요청하면 댓글 입력란에 댓글이 표시됩니다. 하지만 두 개의 서로 다른 BlogPost에 하나의 객체 Comment를 추가 할 수는 없습니다. 이것은 두 개의 다른 객체가 될 것입니다. BlogPost1.comments [0] ._ id가 BlogPost2.comments [0] ._ id와 같지 않습니다. NoSQL이므로 관계가 없습니다. 관계가 필요하면 DB가 아닙니다. –