2017-01-30 4 views
0

몽구스 채우기 기능을 사용하는 데 가장 심각한 문제가 있습니다. 나는 여기에있는 모든 스레드를 보았고 아무 것도 도움이되지 못했습니다. 내 스키마는 아래에 나와 있습니다.몽구스 채우기 방법

'use strict'; 

/** 
* Module dependencies. 
*/ 
var mongoose = require('mongoose'), 
    Schema = mongoose.Schema; 


/** 
* Prodcut Feature Extra 
*/ 
var extra = new Schema({ 
    name: { 
    type: String, 
    default: '', 
    required: 'Please fill Product name', 
    trim: true 
    }, 
    price: { 
    type: Number, 
    default: 0 
    }, 
    description: { 
    type: String, 
    trim: true 
    }, 
    toolTip: { 
    type: String, 
    trim: true, 
    min: 3, 
    max: 140 
    }, 
    created: { 
    type: Date, 
    default: Date.now 
    }, 
    user: { 
    type: Schema.ObjectId, 
    ref: 'User' 
    } 
}); 

/** 
* Product Feature Schema 
*/ 
var feature = new Schema({ 
    name: { 
    type: String, 
    default: '', 
    required: 'Please fill feature name', 
    trim: true 
    }, 
    created: { 
    type: Date, 
    default: Date.now 
    }, 
    user: { 
    type: Schema.ObjectId, 
    ref: 'User' 
    } 
}); 

/** 
* Product Schema 
*/ 
var ProductSchema = new Schema({ 
    name: { 
    type: String, 
    default: '', 
    required: 'Please fill Product name', 
    trim: true 
    }, 
    created: { 
    type: Date, 
    default: Date.now 
    }, 
    features: [feature], 
    extras: [extra], 
    user: { 
    type: Schema.ObjectId, 
    ref: 'User' 
    } 
}); 

mongoose.model('Feature', feature); 
mongoose.model('Extra', extra); 
mongoose.model('Product', ProductSchema); 

은 내가 몽구스 REPL에서 다음 모든 쿼리를 시도했지만 아무것도 사람이 어떤 제안이 있습니까

models.Product.find().populate("Extra", "name").exec() 
 
models.Product.find().populate({path: "extras", location: "Extra"}).exec() 
 
models.Product.find().populate('extras', 'name').exec() 
 
models.Product.find().populate('extras', 'Extra').exec()

작동하지? 이것은 나를 죽이고있다 !!

+0

수단 "하지만 아무것도 작동하지 않습니다"무엇을 보여주십시오 작동합니다. 오류가 있습니까? –

+0

현재 사용중인 몽구스의 버전이 확실치 않지만 Schema.ObjectId를 유효한 유형으로 본 적이 없습니다. "Schema.Types.ObjectId"를 읽어야합니다 - 적어도 스키마 유효성 검사 오류가 발생해야합니다 ... – SylonZero

+0

달성하고자하는 것은 무엇입니까? 당신의 스키마에서'extras'는 레퍼런스가 아니라 임베디드 문서이므로, 그렇게 작동하지 않아야합니다. 당신이 정말로 원하는 것을 설명해 주시면, 우리가 당신을 도울 수 있습니다. –

답변

1

그냥 호기심!

아래의 초기화는 ProductSchema에서 참조하기 전에 수행 할 필요가 없습니까? 이보다이

mongoose.model('Feature', feature); 
mongoose.model('Extra', extra); 

var ProductSchema = new Schema({ 
name: { 
type: String, 
default: '', 
required: 'Please fill Product name', 
trim: true 
}, 
created: { 
    type: Date, 
    default: Date.now 
}, 
features: [ { 
    type: Schema.ObjectId, 
    ref: 'Feature' 
}], 
extras: [ { 
    type: Schema.ObjectId, 
    ref: 'Extra' 
}], 
user: { 
    type: Schema.ObjectId, 
    ref: 'User' 
} 
}); 

같은

mongoose.model('Feature', feature); 
mongoose.model('Extra', extra); 

뭔가

models.Product.find().populate("extras")