代码之家  ›  专栏  ›  技术社区  ›  fct

如何在同一个查询中检索用户的关注者和我在mongodb中已经关注的用户

  •  0
  • fct  · 技术社区  · 6 年前

    我将mongodb用于这个集合:User和Follow。用户集合有用户,follow有followers映射。

    尝试运行此查询,但只获取用户的关注者以及我跟踪的用户:

    users = User.collection.aggregate([{
        "$lookup": {
          from: "follows",
          localField: "_id",
          foreignField: "followable_id",
          as: "follow"
        }
      },
      {
        "$match": {
          "follow.followable_type": "User",
          "follow.user_id": BSON::ObjectId.from_string(userid)
        }
      },
      {
        "$group": {
          _id: "$follow.followable_id",
          count: {
            "$sum": 1
          },
          data: {
            "$addToSet": "$$ROOT"
          },
        }
      },
      {
        "$project": {
          _id: 1,
          data: 1,
          count: 1,
          follow: {
            '$cond': {
              'if': {
                '$eq': ["$count", 2]
              },
              then: true,
              else: false
            }
          }
        }
      },
      {
        "$skip": 0 * 10
      },
      {
        "$limit": 10
      }
    ])
    
      users.each do |user|
        puts "#{user['data'].first['name']}: #{user['follow']}: #{user['count']}"
      end
    

    如何在同一个查询中返回我正在跟踪的用户?

      Diego_Lukmiller: false: 1
      Marianno: false: 1
      kah: false: 1
      Fausto Torres: false: 1
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   fct    6 年前

    我分三步解决/修复了它(用ruby/mongoid编写):

      //get users the user X follows
      users = Follow.collection.aggregate([
        { "$match": { "followable_id": BSON::ObjectId.from_string(userid) } },
        { "$lookup": { from: "users", localField: "user_id", foreignField: "_id", as: "user" } },
        { "$unwind": "$user" },
        { "$sort": {"created_at":-1} },
        { "$skip": page * 10 },
        { "$limit": 10 },
        { "$group":{ _id: "$followable_id", data: {"$addToSet": "$user._id"} } }
      ])
      userids=users.blank? ? [] : users.first['data'].to_a
    
      //get users I follow
      f=Follow.collection.aggregate([
        {"$match":{user_id: BSON::ObjectId.from_string(meid), followable_id: {"$in":userids}}},
        {"$group":{ _id: nil, data: {"$addToSet": "$followable_id"}}}
      ])
    
      //make intersection
      users = User.collection.aggregate([
        { "$lookup": { from: "follows", localField: "_id", foreignField: "user_id", as: "follow" } },
        { "$unwind": "$follow" },
        { "$match": { "follow.followable_type": "User","follow.followable_id": BSON::ObjectId.from_string(userid) } },
        { "$group":{ _id: "$_id", data:{"$addToSet": "$$ROOT"}} },
        { "$unwind": "$data" },
        { "$project": {
            _id: 1,data: 1,count: 1,
            follow: {
              '$cond': {
                "if": { '$in': [ "$_id", f.first['data'] ]  }, "then": true,
                "else": false
              }
            }
          }
        },
        { "$skip": page * 10 },
        { "$limit": 10 }
      ])
    

    输出:

    name: Fausto Torres; Do I follow too? false; Counter that's make the magic: 1
    name: kah; Do I follow too? false; Counter that's make the magic: 1
    name: Marianno; Do I follow too? true; Counter that's make the magic: 2
    name: Diego_Lukmiller; Do I follow too? true; Counter that's make the magic: 2
    

    查询执行得很快,看起来很简单。 有没有办法/如何在一个查询中解决它?!?