2017-11-19 10 views
0

폴링 앱을 만들고 있는데 사용자 ID, IP 주소 또는 MAC 주소를 기반으로 한 설문에 투표 한 사용자를 저장하고 싶습니다.몽구스 - 왜 하위 문서가 작동하지 않습니까?

Poll 모델에 voted: VotedSchema 속성을 사용하면 곧바로 앞으로 나아갈 것이라고 생각했지만 어떤 이유로 작동하지 않습니다.

설문 조사 모델 :

const mongoose = require('mongoose'); 
const { Schema } = mongoose; 
const OptionSchema = require('./Option'); 
const VotedSchema = require('./Voted'); 

const pollSchema = new Schema({ 
    title: String, 
    options: [OptionSchema], 
    dateCreated: { type: Date, default: Date.now() }, 
    _user: { type: Schema.Types.ObjectId, ref: 'User' }, 
    voted: VotedSchema 
}); 

mongoose.model('polls', pollSchema); 

투표 subdoc : 다른 한편으로는, 나는 subdoc를 사용하지 않는 경우

const mongoose = require('mongoose'); 
const { Schema } = mongoose; 

const votedSchema = new Schema({ 
    userID: [String], 
    IPaddress: [String], 
    MACaddress: [String] 
}); 

module.exports = votedSchema; 

는, 그것은 잘 작동 :

const mongoose = require('mongoose'); 
const { Schema } = mongoose; 
const OptionSchema = require('./Option'); 
const VotedSchema = require('./Voted'); 

const pollSchema = new Schema({ 
    title: String, 
    options: [OptionSchema], 
    dateCreated: { type: Date, default: Date.now() }, 
    _user: { type: Schema.Types.ObjectId, ref: 'User' }, 
    voted: { 
    userID: [String], 
    IPaddress: [String], 
    MACaddress: [String] 
    } 
}); 

mongoose.model('polls', pollSchema); 

나는 무엇인가 놓치고있다. 슈퍼 간단 여기? 이 문제는 다소 당혹 스럽습니다.

+0

당신이 명확히 수, 당신은 참조 하위 문서의 경우에는 작동하지 않는 무엇을 의미합니까? –

답변

0

이것은 아직 몽구스에서 구현되지 않았습니다.

이것을 확인하십시오. 당신이하고있는 것처럼 이제 당신이 원하는 경우에 대한

https://github.com/Automattic/mongoose/pull/585

, 당신은 두 번째 옵션을 할 수 있습니다. 문서

또는 사용

VotedSchema.Type 또는 VotedSchema.tree

임베딩은 어레이에 의해 수행 될 수있다.

투표 : [VotedSchema]

http://mongoosejs.com/docs/subdocs.html#single-embedded

+0

그게 도움이됩니까? –