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 creates 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
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
I have already asked the question with MongoDB community but not get proper help, please review below link for the community reference
Better I will explain it as steps
Step 1: match with user collections: There you will get user details
Step 2: lookup with product collections: with that, you will get the user-created all products.
Step 3: match for search, if there any search or filter needed to match this with product name
example: (Hopefully this may help you)
async function search(userid, query = null)
{
return await userModel
.aggregate()
.match({
_id: userid
})
.lookup({
from: "products",
localField: "_id",
foreignField: "userid",
as: "products"
})
.match({ $text: { $search: "cake" } })
.exec();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.