Search in document and in referenced documents

Hello,

Considering the following mongoose schemes, the first one containing the Referenced document:

const Recipient = Schema({
  _id: Schema.Types.ObjectId,
  name: String,
  surname: String,
  address: { type: Schema.Types.ObjectId, ref: 'Address' }
});
const Address = Schema({
  _id: Schema.Types.ObjectId,
  street: String,
  number: Number,
  city: String
});

Question #1: In this given scenario what would be the correct way to formulate the query to find:

All the recipients {name: ‘Jane’, surname: ‘Doe’} with an address {street: ‘Test Street’, number: 20, city: ‘Sunshine city’} ?

Note: While some kind of relationship between these 2 schemes might be observed, thus making recipients embedded inside an address look seemingly attractive, for the purposes beyond the scope of this example they need to be maintained as 2 separate documents. That being said, I am not sure if duplicating and maintaining the same data in both structures (embedded and reference) could be beneficial.

1 Like