How to index/search referenced documents from another collection?

Hey,

So I’ve basically got a schema for a message board type application, where there are threads with referenced comments from another collection in the same database. The schema looks like this:

const threadSchema = new Schema(
  {
    title: String,
    content: String,
    name: String,
    email: String,
    date: { type: Date, required: true },
    company: String,
    comments: [
      {
        type: Schema.Types.ObjectId,
        ref: "Comments",
      },
    ],
  }
);

and then comments:

const CommentsSchema = new Schema({
  content: String,
  name: String,
  email: String,
  date: Date,
});

Is there a way for me to create a search index that will also populate the info from the comments collection and search across the 2 merged collections?

Thanks,
Andre

Hi @andre_c,

Just to clarify, are you talking about an Atlas Search index here? If so, I am wondering if the How to Run Atlas Search Queries Across Collections documentation suits your use case(s).

Additionally, I’m curious about the “populate the info from the comments collection” portion of your statement - Do have an example you can detail of what you’re trying to achieve here?

Regards,
Jason

Hey,

Thanks for the reply!

It is indeed an atlas search index im referring to.
I’ve tried to get $lookup queries to run, but I can’t get the query path to recognise the comments array for each thread.

Perhaps I explained myself poorly. I mean that as comments on each thread are a reference using objectID they might need populating in order to be searched through.

I just want the returned results of a search to include any hits from the actual content in the referenced comments on each main thread.

Thanks,
Andre

Thanks for providing that information and clarification Andre - I think I have a better idea of what you’re trying to achieve now.

Could you provide a few more details so that I can see why it may not be recognising the "comments" array for each "thread":

  1. The Atlas search index definition on each collection (You can obtain this using the JSON editor in the Atlas UI).
  2. Sample document(s) from each collection, most preferably having matching fields / reference values for the $lookup
  3. Expected output from these sample document(s)
  4. The actual aggregation pipeline you’ve attempted so far.

Regards,
Jason