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