内の文書から特定のフィールドを選択しますこんにちは、私は、ユーザーとの対話からPolleeとpupulateフィールドでクエリを実現する必要がありますが、相互作用から、私は唯一のステータスフィールドを必要とし、この分野のマングース
var interactionSchema = new Schema({
pollee: { type: ObjectId, ref: 'Pollee' },
answers: { type: [ObjectId], ref: 'Answer', autopopulate: true },
status: type: String
});
var PolleeSchema = new Schema({
firstName: String,
lastName: String,
gender: String,
user: { type: ObjectId, ref: 'User', required: true },
interactions: { type: [ObjectId], ref: 'Interaction', autopopulate: true }
});
var userSchema = new Schema({
email: String,
pollee: { type: Schema.Types.ObjectId, ref: 'Pollee', autopopulate: true }
});
で3つのスキーマを持っています、私は答えと他のObjectIDの配列を必要としないで、相互作用colectionを含んでいます。
私はこのクエリPollee.find(req.body.filters)
.select('id age birthday country device_register gender municipality state parish user interactions.status')
.populate('user','email createdAt')
.exec(function(err,pollees){
//Other
}
//....
この戻り相互作用の配列をしたが、ステータスフィールドは返されません。選択項目にインタラクションのみを書き込むと、インタラクションからのすべてのフィールドとのインタラクションが返されます。
私は相互作用からのみStatusフィールドを必要とする相互作用
からこの戻り、何もこの
Pollee.find(req.body.filters)
.select('id age birthday country device_register gender municipality state parish user')
.populate('user','email createdAt')
.populate('interactions', 'status')
.exec(function(err,pollees){
//Other
}
//....
を試していません。
ここでの問題は、Pollee Schemaのインタラクションフィールドです。このフィールドはObjectsIdのArrayで、このフィールドはSchemaからオートポピュレートされています。インタラクションからステータスフィールドのみを選択したい
ご存じですか?
[マングース:深い人口(人口フィールドを移入)]の可能な重複(http://stackoverflow.com/questions/18867628/mongoose-deep-population-a-populated-a-populated-field) – willie17
@TrietDangこの解決策は私にとってはうまくいく。ここで問題となるのは、Pollee Schemaのインタラクションフィールドです。このフィールドはObjectsIdの配列です。このフィールドは、Schemaからオートポピュレートされています。私は対話からステータスフィールドのみを選択したい – ZizouJd
申し訳ありませんが、あなたはmongoose-autopopulateプラグインを使用していると述べていません。だから私はあなたのスキーマを見落としました。 – willie17