Elasticsearch and subsequent Mongodb queries

☆樱花仙子☆ 提交于 2019-12-06 15:41:06

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)

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.)

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