0

In the context of metabase:

I am applying the following one-liner query to extract a particular field from a collection of mongodb documents:

[{"$project":{"myName":"$field1.field2" }},
    {"$match":{"_id":{"$eq":"blah"}}}      ]

myName is a string field that is in fact a serialized JSON object.

How can I adapt the pipeline above, perhaps using JSON.parse(), to pull out fields nested further down the JSON hierarchy?

Part II: How would I add a final step to extract some of those deeply nested fields? The situation is further complicated because the document structure is not consistent between documents...

Thanks!

2
  • "How can I adapt the pipeline above, perhaps using JSON.parse().." - Aggregation pipelines do not run JavaScript functions. There are no aggregation operators to parse JSON in any way. The bigger question is just "why" would you store a "JSON string" in a field in MongoDB? Why did you not simply JSON.parse() before you stored it? Because if you did, then MongoDB can manipulate it just like any other document. But not as a "string". This really sounds more like a hangover from an RDBMS implementation that should have been converted earlier. Commented Mar 4, 2019 at 11:01
  • Also $unwind as the very first pipeline stage is very wasteful. You should almost always begin with $match. Commented Mar 4, 2019 at 11:03

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.