1

Explnation : how we can concate the time and sv . convert normalizaed the array stract

{
    "data": [
      {
        "time": "1998-08-31T03",
        "sv": {
          "05": {
            "13": 0,
            "14": 1
          }
        }
      }
    ]
  }

For example expected document will be.


{
 "data" : {
    "1998-08-31T03:05:13Z": 0,
    "1998-08-31T03:05:14Z": 1,
}
}

2 Answers 2

1
  • $reduce to iterate loop of data array
  • $objectToArray convert sv object to key-value array
  • $map to iterate loop of sv array
  • $objectToArray convert minute object to key-value array
  • $map to iterate loop of minutes array
  • $concat to concat the required format of date parts
  • $arrayToObject convert key-value pair to object
  • $concatArrays to concat data elements
  • $reduce to iterate loop of data array
  • $reduce to iterate loop of nested data elements
  • $mergeObjects to merge date objects
db.collection.aggregate([
  {
    $project: {
      data: {
        $reduce: {
          input: "$data",
          initialValue: [],
          in: {
            $concatArrays: [
              "$$value",
              {
                $map: {
                  input: { $objectToArray: "$$this.sv" },
                  as: "s",
                  in: {
                    $map: {
                      input: { $objectToArray: "$$s.v" },
                      as: "s1",
                      in: {
                        $arrayToObject: [
                          [
                            {
                              k: {
                                $concat: ["$$this.time", ":", "$$s.k", ":", "$$s1.k", "Z"]
                              },
                              v: "$$s1.v"
                            }
                          ]
                        ]
                      }
                    }
                  }
                }
              }
            ]
          }
        }
      }
    }
  },
  {
    $project: {
      data: {
        $reduce: {
          input: "$data",
          initialValue: {},
          in: {
            $mergeObjects: [
              "$$value",
              {
                $reduce: {
                  input: "$$this",
                  initialValue: {},
                  in: { $mergeObjects: ["$$value", "$$this"] }
                }
              }
            ]
          }
        }
      }
    }
  }
])

Playground

Sign up to request clarification or add additional context in comments.

Comments

0
db.collection.aggregate([
  {
    $unwind: "$data"
  },
  {
    $addFields: {
      "data.sv": {
        $objectToArray: "$data.sv"
      }
    }
  },
  {
    $unwind: "$data.sv"
  },
  {
    $addFields: {
      "data.sv.v": {
        $objectToArray: "$data.sv.v"
      }
    }
  },
  {
    $unwind: "$data.sv.v"
  },
  {
    $addFields: {
      "data": {
        $arrayToObject: [
          [
            {
              k: {
                $concat: [
                  "$data.time",
                  ":",
                  "$data.sv.k",
                  ":",
                  "$data.sv.v.k",
                  "Z"
                ]
              },
              v: "$data.sv.v.v"
            }
          ]
        ]
      }
    }
  },
  {
    $group: {
      _id: null,
      data: {
        $mergeObjects: "$data"
      }
    }
  }
])

MongoPlayground

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.