MongoDB: a project using array fields

I have such a collection: Each document contains a field Message, which in turn contains an array Fields. Each document under the array Fieldshas properties Valueand Name.

[
    {
        "_id" : ObjectId("55711efed0103b1598140076"),
        "Message" : {
            "Fields" : [ 
                {
                    "Value" : 131,
                    "Name" : "Options",
                }, 
                {
                    "Value" : 8,
                    "Name" : "Length",
                }       
            ]
        }
    },

    {
        "_id" : ObjectId("55711efed0103b1598140077"),
        "Message" : {
            "Fields" : [ 
                {
                    "Value" : 65,
                    "Name" : "Options",
                }, 
                {
                    "Value" : 13,
                    "Name" : "Length",
                },
                {
                    "Value" : 101,
                    "Name" : "Width",
                }                   
            ]
        }
    }
]

After searching for documents using, db.Collection.find({})I would like to design such that: he analyzes each Fieldunder Message.Fieldsand designs them in a new document, using Typeboth the name of the property and Valuethe value. The result will look like this:

[
    {
        "_id" : ObjectId("55711efed0103b1598140076"),
        "Options" : 131,
        "Length" : 8
    },
    {
        "_id" : ObjectId("55711efed0103b1598140077"),
        "Options" : 65,
        "Length" : 13,
        "Width" : 101
    },
]

Is this achievable with function()or aggregateor in any other way in MongoDB?

+4
source share
3 answers

MongoDB 3.4.4 $arrayToObject "$Message.Fields" , , k var, $map. $arrayToObject /.

_id , $replaceRoot.

:

db.collection.aggregate([
    { "$replaceRoot": {
        "newRoot": {
            "$mergeObjects": [
                { _id: "$_id" },
                { "$arrayToObject": {
                    "$map": {
                        "input": "$Message.Fields",
                        "in": {
                            "k": "$$this.Name",
                            "v": "$$this.Value"
                        }
                    }
                } }
            ]
        }
    } }
])

map() find() , , , :

var result = db.collection.find().map(function (doc){
    var obj = {};
    obj["_id"] = doc._id
    doc.Message.Fields.forEach(function (field){
        obj[field.Name] = field.Value;
    })
    return obj;
});
printjson(result);

:

[
    {
        "_id" : ObjectId("55711efed0103b1598140076"),
        "Options" : 131,
        "Length" : 8
    },
    {
        "_id" : ObjectId("55711efed0103b1598140077"),
        "Options" : 65,
        "Length" : 13,
        "Width" : 101
    }
]
+1

aggregation $cond

db.collection.aggregate(
    [
      { $unwind: "$Message.Fields" },
      { $project: { fields: "$Message.Fields" }}, 
      { $project: 
          { 
              Option: 
                {
                  $cond: [{ $eq: [ "$fields.Name", "Options" ]}, "$fields.Value", 0 ]
                }, 
              Length: 
                {
                  $cond: [{ $eq: [ "$fields.Name", "Length" ]}, "$fields.Value", 0 ]
                }, 
              Width: 
                { 
                  $cond: [{ $eq: [ "$fields.Name", "Width" ]}, "$fields.Value", 0]
                }
          }
        }, 
        { $group: { 
                    _id: "$_id", 
                    Options: { $sum: "$Option" }, 
                    Width: { $sum: "$Width" }, 
                    Length: { $sum: "$Length" }
                  }
         } 
    ]
)

:

{
        "_id" : ObjectId("55711efed0103b1598140077"),
        "Options" : 65,
        "Width" : 101,
        "Length" : 13
}
{
        "_id" : ObjectId("55711efed0103b1598140076"),
        "Options" : 131,
        "Width" : 0,
        "Length" : 8
}
+3

I use a follwing script to generate the result with mongo shell.

var result=[];

var cursor=db.collection.find().forEach( 

function(myDoc) 
{ 
    print(myDoc)

        var obj1={};
        obj1._id=myDoc._id;
        var len_of_fields=myDoc.Message.Fields.length;

        if(len_of_fields==2)
        {
          var j=0;         
          obj1.Options=myDoc.Message.Fields[j].Value;    
          obj1.Length=myDoc.Message.Fields[j+1].Value;
          result.push(obj1)  
        }
        else if(len_of_fields==3)
        {

             var j=0;
            obj1.Options=myDoc.Message.Fields[j].Value;
            obj1.Length=myDoc.Message.Fields[j+1].Value;
            obj1.Width=myDoc.Message.Fields[j+2].Value;

              result.push(obj1)
        }   

    print( "DOC: ", result); 

} 

);
+1
source

All Articles