Elasticsearch and subsequent Mongodb queries

梦想的初衷 提交于 2019-12-08 06:36:10

问题


I am implementing search functionality using Elasticsearch.

I receive "username" set returned by Elasticsearch after which I need to query a collection in MongoDB for latest comment of each user in the "username" set.

Question: Lets say I receive ~100 usernames everytime I query Elasticsearch what would be the fastest way to query MongoDB to get the latest comment of each user. Is querying MongoDB 100 times in a for loop using .findOne() the only option?

(Note - Because latest comment of a user changes very often, I dont want to store it in Elasticsearch as that will trigger retrieve-change-reindex process for the entire document far too frequently)


回答1:


This answer assumes following schema for your mongo db stored in comments db.

{
  "_id" : ObjectId("5788b71180036a1613ac0e34"),
  "username": "abc",
  "comment": "Best"
}

assuming usernames is the list of users you get from elasticsearch, you can perform following aggregate:

a =[
    {$match: {"username":{'$in':usernames}}},
    {$sort:{_id:-1}},
    {
       $group:
         {
           _id: "$username",
           latestcomment: { $first: "$comment" }
         }
     }
]
db.comments.aggregate(a)



回答2:


You can try this..

db.foo.find().sort({_id:1}).limit(100);

The 1 will sort ascending (old to new) and -1 will sort descending (new to old.)



来源:https://stackoverflow.com/questions/38389628/elasticsearch-and-subsequent-mongodb-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!