Atlas Search with populate in aggregation pipeline with text index

I have one query regarding search implementation. Here search implementation or atlas-search is best suitable for search in a single collection. I think normally we faced a scenario where we need to search with populate or ref collection also. How can I do it with better implementation?

Consider the very simple scenario where I have 2 collections.
1 Product (text index of product name field)
2 User (text index on username fields may be firstName, lastName etc)

The user create the products so in product collection there is ref on **createdBy** with user’s MongoID. On Frontend, there is a listing of products so I need to provide search criteria like below in single text search

  1. I can search with product name
  2. I can search with the name of the owner of the product

What is the best way to handle such a scenario with MongoAtlas? ex. I want all products created by “Donni Bachhan”. what is the aggregation pipeline you just or best practise for it

Hi Jaydipsinh. This is a really interesting question. Several different approaches depending on your use case and data.
If a product is created by a user, can that data (user name, id) be embedded in the product document? That seems simple.
You can only create an Atlas Search index per collection, so you can find whatever documents you need, and then $lookup from the same aggregation pipeline at a later stage to the other collection… or leave the aggregation and reference the second collection.
Also, do you have lots of products with complicated names? If not, you could potentially avoid Search and just consider using relationships within data in GraphQL. Or use $search in conjunction with GraphQL. $search to find the product. Then use a filter to GraphQL endpoint enabled by Realm to get info about that product and the owners of it in 1 call. I talk about relationships in GraphQL in this blog: https://www.mongodb.com/how-to/graphql-easy.
I know these are lots of options. Hope this helps.

Thanks, Karen for your reply

I embedded the user information into the product document. Due to that if the user updates their information I need to update all product created by the user, which is I personally think no the good way but I need to follow that anyhow.

I don’t have experience with GraphQL but will try to do something with that. as of now, my product is already in the live market so I don’t have much time to so.

Hi, Jaydipsinh, I would suggest that you can very easily have a database trigger set on your user collection, so that if the user document changes, it will execute a function that will automatically update those fields in the product collection. Yours is an excellent use case for this. You basically set it, and forget it.

Here is a quick 5 minute video about triggers and functions: https://www.youtube.com/watch?v=crcTRpkK2tg
and the documentation for triggers is here: https://www.youtube.com/watch?v=crcTRpkK2tg.

Have a look. I think this will really help you. Let me know if you have questions about it.

Thanks.

Ahh, That seems interesting but is there any impact on database performance? just want to know for the curiosity.

I think GraphQL is interesting one but need to invest sometime to learn it. with GraphQL is that possible to quering to relation collection field? line If I am quering to Product collection but get the based on user name from User collection. is that possible? If you remember my case I need to text search on either Product name or user name. we can manage that in single query with GraphQL?

Will try to implement Trigger once I sure with performance imapct of it on database or may be on query.

Thanks for your suggestion

Hi! It will not affect your database performance. The triggers that you configure will fire off a function that you write. These functions execute serverlessly, independent of your database - and they only run when they are used. https://docs.mongodb.com/realm/functions - and they take no time to set up.

I am a big fan of the GraphQL stuff. I sent you the link to a blog article, but it is also a video with a real example I work through if that is easier to consume: https://youtu.be/nM51I6XVlTw

Keep me posted. I am looking forward to hearing if these help you.

Thanks for your help Karen, Will try to figure out with GraphQL to bind the proper solution.
I really appreciate your efforts on it. thanks gain…

And yeah one more thing now I am following you on Twitter “youoldMaid” right?

1 Like

Yes @JD_Old7, she is on twitter with the same username which is mentioned by you.

hi guys! Yes, indeed!! Followed you back!

Tiwitter
Facebook
LinkedIn
Instagram

Hey Karen,

I am trying to implement GraphQL setup. I have done successfully…! Now I am trying to call the data using GraphiQL

I am unable to pass limit params to my query. I don’t know how to solve that

Is that anything I am missing over here?

Sorry adding this to the current thread…

hi, JD! Glad to see how much progress you are making so fast! It looks like you have a misspelling in “limit.” Add an ‘i’ and it should work. Let me know. Karen

Yes, there was an issue. but that was due to n numbers of try to sort this out.

But the real problem with collection name “space” it should be the “spaces” so that’s the problem.

I have some of good scenarios with my real world application with graphQL and how to cover it with our queries. How can we short this out it’s a question actually. I am happy to share scenarios if you think to manage something on it.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.