MongoDB嵌套聚合(join)

时间:2018-01-27 22:31:43

标签: arrays mongodb join aggregation

我有一个文档要运行嵌套聚合,但我不知道如何运行此操作。

内容文档(删除了不必要的字段):

{
    "_id" : ObjectId("5a6b8b734f1408137f79e2cc"),
    "reviews" : [ 
        {
            "_id" : ObjectId("5a6cf7c41562160494238781"),
            "headline" : "",
            "body" : "",
            "likeUsers" : [ 
                {
                    "_id" : ObjectId("5a6b5f2e1fc2a11c5c83a6e2")
                }
            ],
            "dislikeUsers" : [],
            "isCritic" : false,
            "isSpoilers" : false,
            "isTop" : true,
            "rate" : 3,
            "userId" : ObjectId("5a6b5f2e1fc2a11c5c83a6e2"),
            "date" : Timestamp(1517090823, 1)
        }
    ]
}

用户内容:

{
    "_id" : ObjectId("5a6b5f2e1fc2a11c5c83a6e2"),
    "username" : "",
    "password" : "",
    "firstname" : "",
    "lastname" : "",
    "email" : "",
    "mobile" : "",
    "tel" : "",
    "nationalCode" : "",
    "gender" : "male",
    "birthdate" : Timestamp(0, 1),
    "promotionCode" : "12131SKSQ",
    "status" : 1,
    "created" : Timestamp(1516986633, 1),
    "updated" : Timestamp(1516986633, 2)
}

我希望从likeUsers数组字段中获取所有用户信息。

3 个答案:

答案 0 :(得分:0)

您可以使用$lookup聚合将喜欢的用户信息作为嵌入式数组

db.content.aggregate(
    [
        {$match : {"_id" : ObjectId("5a6b8b734f1408137f79e2cc")}},
        {
            $lookup : {
                from : "user",
                localField : "reviews.likeUsers._id",
                foreignField : "_id",
                as : "likeUsersInfo"
            }
        }
    ]
)

修改-1

将嵌入的文档放在同一层次结构中

db.content.aggregate(
    [
        {$match : {"_id" : ObjectId("5a6b8b734f1408137f79e2cc")}},
        {
            $lookup : {
                from : "user",
                localField : "reviews.likeUsers._id",
                foreignField : "_id",
                as : "likeUsersInfo"
            }
        },
        {$addFields : {"reviews.likeUsers" : "$likeUsersInfo"}},
        {$project : {likeUsersInfo : 0}}
    ]
).pretty()

答案 1 :(得分:0)

非常感谢您的回复。可以在$ ./bin/getchar_parse_matrix <dat/matrix.csv 000000 000100 010100 001100 000000 000000 $ ./bin/getchar_parse_matrix <dat/matrix2.csv 00000010 00010001 01010010 00110001 00000011 00000000 11100111 00011000 中准确获取用户信息吗?我需要这样的返回结果:

$output=[];    
$i=0; 
$html='';
while($row = $stmt->fetch()) {    

  if($i % 3 == 0) {
    $html .= "<div class='row'>\n";
  }

$html .="<div class='col-sm-4'>
        <div class='room-box'>
          <img src='$thumb' class='img-responsive' >
          <h4>$name</h4>
        </div>
      </div>\n";

   if($i % 3 == 2) { 
      $html .= "</div>\n";
      $output[] = $html;
      $html = '';
   }
  $i++;
}
if($i%3!=0){
  $html .= "</div>\n";
  $output[] = $html;
  $html = '';
} 

答案 2 :(得分:0)

最后,我可以使用public class ExampleModule { static ExampleModule instance; public static ExampleModule getInstance() { if(instance == null) instance = new ExampleModule(); return instance; } public void method1(){ // do somthing } } $addFields命令获取准确标签中的用户信息。我为有限的用户信息字段添加了另一个$project。操作运行成功和reviews.likeUsers。[用户]字段有限但内容字段不显示。

新查询:

$project

运行以上查询后返回以下字段和其他内容字段不显示:

db.contents.aggregate(
    // Pipeline
    [
        // Stage 1
        {
            $lookup: // Equality Match
            {
                            from : "users",
                            localField : "reviews.likeUsers._id",
                            foreignField : "_id",
                            as : "likeUsersInfo"
            }
        },

        // Stage 2
        {
            $addFields: {"reviews.likeUsers" : "$likeUsersInfo"}
        },

        // Stage 3
        {
            $project: {likeUsersInfo : 0}
        },

        // Stage 4
        {
            $project: {"reviews.likeUsers.username": 1,"reviews.likeUsers.firstname": 1,"reviews.likeUsers.lastname": 1}
        },

    ]
);