1

I have two mongoose schemas,

Schema A:

{
    field1 : { type : [String] }

}

Schema B:

{
    field2 : { type : ObjectId, ref : 'A' }
}

I want to populate mongoose in such a way that i get the following result :

{
    field2 : field1 of a documnet from A 
}

or

{
    field2 : document of A,
    field1 : field1 field of document A
}

Example:

Document in A { _id : 1 field1 : ["1","2","3"] }

Document in B

{
    _id : 2
    field2 : 1
}

After i populate i want the result to be

{
    _id : 2
    field2 : ["1","2","3"]
}

or

{
    _id : 2
    field2 : {_id : 1 , field1 : ["1","2","3"]}
    field1 : ["1","2","3"]
}

Any one of the two will be appreciated.

1 Answer 1

1

This can be achieved using vitual fields in mongoose.

first populate('field2') then create a virtual field field1 and set that equal to field2.field1

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.