MongoDB: get documents by tags

后端 未结 3 680
执笔经年
执笔经年 2020-12-15 07:25

I\'ve got documents containing tags array. I want to provide tags based recommendations on site, so I need to get documents containing same tags + documents tha

3条回答
  •  猫巷女王i
    2020-12-15 08:26

    Steps:

    1. Find matching products which contains any of the specified keys.

    2. Unfold on keys

    3. Do find again to filter unwanted after unfolding

    4. Group them by adding key occurrence

    5. Sort desc to get most relevant first

    [{ "$match" : { "keys" : { "$in" : [ { "$regex" : "text" , "$options" : "i"}]}}}, { "$unwind" : "$keys"}, { "$match" : { "keys" : { "$in" : [ { "$regex" : "text" , "$options" : "i"}]}}}, { "$group" : { "_id" : { "productId" : "$productId"} , "relatedTags" : { "$sum" : 1} }}, { "$sort" : { "relatedTags" : -1}},{ "$limit" : 10}]

提交回复
热议问题