0

Hi I have three Schemas with this field's

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

I need to realize a query in Pollee and pupulate fields from user and interactions, but from interactions I only need the status field, I dont need answers and other ObjectID arrays that contain the Interactions colection.

I made this Query

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
        }
       //....

This return interactions array but status field is not returned. If I write only interactions on the select, return interactions with all fields from interactions.

I try this

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
        }
       //....

This return nothing from interactions

I need only Status field from interactions.

The problem here is the interactions field from Pollee Schema, this field its a Array of ObjectsId, and this field its autopopulated from Schema. I want to select only the status field from interactions

Any idea?

ZizouJd
  • 79
  • 3
  • 12
  • Possible duplicate of [Mongoose: deep population (populate a populated field)](http://stackoverflow.com/questions/18867628/mongoose-deep-population-populate-a-populated-field) – willie17 Dec 10 '16 at 19:29
  • @TrietDang This solution doenst work for me. The problem here is the interactions field from Pollee Schema, this field its a Array of ObjectsId, and this field its autopopulated from Schema. I want to select only the status field from interactions – ZizouJd Dec 10 '16 at 20:31
  • Sorry, you didn't state that you use mongoose-autopopulate plugin. So I overlooked your schema. – willie17 Dec 11 '16 at 22:09

1 Answers1

0

I see that you are using mongoose-autopopulate.

It is stated in their document that you can do like this:

var PolleeSchema = new Schema({
  //...
  interactions: { type: [ObjectId], ref: 'Interaction', autopopulate: { select: 'status' } }
});
willie17
  • 901
  • 8
  • 12
  • I can do this in the query? I want in the query select only the specific fields from interactions Schema. – ZizouJd Dec 12 '16 at 01:46
  • Your solution its fine but afects other controllers or functions. I want select fields from interactions only in the query not in the Schema – ZizouJd Dec 12 '16 at 03:11
  • So, I recommend stop using auto population. Because that always populates the document and you cannot use with "original" population. If you want to population it in the query, your best bet is populate it your self. – willie17 Dec 12 '16 at 04:58