46

I've got a schema that looks a bit like:

var conversationSchema = new Schema({
    created: { type: Date, default: Date.now },
    updated: { type: Date, default: Date.now },
    recipients: { type: [Schema.ObjectId], ref: 'User' },
    messages: [ conversationMessageSchema ]
});

So my recipients collection, is a collection of object id's referencing my user schema / collection.

I need to populate these on query, so i'm trying this:

Conversation.findOne({ _id: myConversationId})
.populate('user')
.run(function(err, conversation){
    //do stuff
});

But obviously 'user' isn't populating...

Is there a way I can do this?

4 Answers 4

125

For anyone else coming across this question.. the OP's code has an error in the schema definition.. it should be:

var conversationSchema = new Schema({
    created: { type: Date, default: Date.now },
    updated: { type: Date, default: Date.now },
    recipients: [{ type: Schema.ObjectId, ref: 'User' }],
    messages: [ conversationMessageSchema ]
});
mongoose.model('Conversation', conversationSchema);
Sign up to request clarification or add additional context in comments.

5 Comments

You sir just saved me a world of pain. tips hat
Good way ! but how can you do an array of unique object id? With no duplicate?
{ type: Schema.ObjectId, ref: 'User' , unique: true }
@MuliYulzary, I don't think that's right based on the docs right here. They claim it's not a validator but only helps in the creation of MongoDB indexes.
not working. returning empty array. my mongoose v5.11.14
45

Use the name of the schema path instead of the collection name:

Conversation.findOne({ _id: myConversationId})
.populate('recipients') // <==
.exec(function(err, conversation){
    //do stuff
});

1 Comment

my mongoose v5.11.14
1

In my case I was using NestJS and I had schema Prop like this:

  @Prop({
    type: [{ type: Types.ObjectId, ref: 'Question' }],
    select: false,
  })
  // This shouldn't necessarily be Question[] as typescript cries
  questions: Types.ObjectId[] 

and changing this schema Prop to this resolved my issue:

  @Prop({
    type: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Question' }],
    select: false,
  })
  questions: mongoose.Schema.Types.ObjectId[]

Comments

1

Player Schema

@Schema()
export class Player {
  @Prop({ required: true })
  firstName: string

  @Prop({ required: true })
  lastName: string
}

Team Schema

@Schema()
export class Team {
  @Prop({ required: true })
  name: string;

  @Prop({ type: [Types.ObjectId], ref: 'Player', required: true })
  players: Types.ObjectId[];
}

Earlier I was doing wrong like the following and unable to populate the list

@Prop({ type: [{ type: Types.ObjectId, ref: 'Player' }], required: true })
players: Types.ObjectId[];

This is how I managed to work this:

@Prop({ type: [Types.ObjectId], ref: 'Player', required: true })
players: Types.ObjectId[];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.