Displaying Nested Documents

Hi Experts, I am new to MongoDB.
I need help to query and project nested documents.

Consider the following sample documents of a collection, CPU load is a nested document its field names vary in different records.

{
    "time": ISODate("2022-08-12T11:05:00Z"),
    "nodeName": "selipcnsgsnmme174",
    "cpuload": {
        "1_11(FSB)": 0.04,
        "1_15(NCB)": 0.04,
        "1_9(NCB)": 0.01,
        "2_1(AP/LC/SS7_SCTP_DP)": 0.13,
        "2_2(AP/LC/SS7_SCTP_DP)": 0.12,
        "2_1(LC)": 0,
        "2_2(LC)": 0
    }
}
{
    "time": ISODate("2022-08-12T11:10:00Z"),
    "nodeName": "selipcnsgsnmme174",
    "cpuload": {
        "1_11(FSB)": 0.04,
        "1_15(NCB)": 0.04,
        "1_9(NCB)": 0,
        "2_1(AP/LC/SS7_SCTP_DP)": 0.13,
        "2_2(AP/LC/SS7_SCTP_DP)": 0.12,
        "2_1(LC)": 0.01,
        "2_2(LC)": 0
    }
}

I would like to project is like the following:

{
    "time": ISODate("2022-08-12T11:05:00Z"),
    "nodeName": "NODE2",
    "2_11(FSB)": 0.04,
    "2_15(NCB)": 0.04,
    "2_9(NCB)": 0.01,
    "2_1(AP/LC/SS7_SCTP_DP)": 0.13,
    "2_2(AP/LC/SS7_SCTP_DP)": 0.12,
    "2_1(LC)": 0,
    "2_2(LC)": 0
}
{
    "time": ISODate("2022-08-12T11:10:00Z"),
    "nodeName": "NODE1",
    "_11(FSB)": 0.04,
    "1_15(NCB)": 0.04,
    "1_9(NCB)": 0,
    "1_1(AP/LC/SS7_SCTP_DP)": 0.13,
    "1_2(AP/LC/SS7_SCTP_DP)": 0.12,
    "1_1(LC)": 0.01,
    "1_2(LC)": 0
}

I am not sure what you mean to project is like following so let me guess two scenario

if you want to change the documents format in the database,
you can do it by renaming the fields with following operator,

db.collection('').updateMany({},$rename{ "cpuLoad.1_11(FSB)" : "1_11(FSB)", ...}

or if you want to display the data after query you can do it throught this

db.collection('').aggregate([{
$project:{
time: 1,
nodeName: 1,
"1_11(FSB)": "$cpuLoad.1_11(FSB)" :
}}])

Thankyou for the reply,

I do not want to change the document format in the database , only need the formatting in projection query.

1_11(FSB) is not common field for all the documents , it will change in documents. Can we have a generic query.

This type of data cosmetic is better done on the application side. The operation is easier to scale since only the application making the query is impacted.

Try with the pipeline

pipeline = [
  { "$replaceRoot" : {
    newRoot : { "$mergeObjects" : [ "$$ROOT" , "$cpuload" ] } }
  },
  { "$unset" : [ "cpuload"] }
]

Take a look at Building with Patterns: The Attribute Pattern | MongoDB Blog. The way your data is structure you cannot benefit indexing for queries like:

{ "cpuload.1_11(FSB}" : 0.04 }

without indexing each and every cpuload fields.

Doing it in aggregation is not doing it application side - your pipeline in fact correctly does it server-side where only the desired format is returned…

Asya

Thanks, but I really know that. It was not clear that the first paragraph was my recommendation and the pipeline was not my recommendation but the answer to the question.