sql >> データベース >  >> NoSQL >> MongoDB

ドキュメントをネストされた配列とネストされた配列とマージします

    $reduce を使用できます $concatArrays を使用 データ構造をフラット化してから、 $unwind> $replaceRoot を使用 ドキュメントごとに単一のメンバーを取得するには:

    db.collection.aggregate([
      { "$project": {
        "members": {
          "$concatArrays": [
            [{ "userID": "$userID", "userType": "$userType" }],
            { "$reduce": {
              "input": "$clients",
              "initialValue": [],
              "in": {
                "$concatArrays": [
                  "$$value",
                  [{ "userID": "$$this.userID", "userType": "$$this.userType" }],
                  "$$this.members"
                ]
              }
            }}
          ]
        }
      }},
      { "$unwind": "$members" },
      { "$replaceRoot": { "newRoot": "$members" }}
    ])
    

    Mongo Playground



    1. Redisスキャンカウント:パターンに一致するすべてのキーをSCANに返すように強制するにはどうすればよいですか?

    2. mongo-c-driverの例をコンパイルできません

    3. NodeJS + Mongo:存在しない場合は挿入、そうでない場合は-更新

    4. php_iniがphp.iniの場所に設定されていません