0

In this addToSet "unique":"controller", "module" and "variable" are unique fields, if any one fields change means it will insert full "unique" and its working fine, but i need to insert if unique fields remain same means i need to insert new "cal"array off "Datetime" and "value",how to i do that?

db.measurement.update({"siteId":"55","thingId":"IOT",
    "unique.controller":11.0,"unique.module":10.0,"unique.variable":"curent"},
    {$addToSet:{
        "unique": {
        "controller":11,
        "module": 10.0,
        "variable" : "curent",
        "calculation" : [ 
                {
                    "min" : "a",
                    "max" : "c",
                    "avg" : "",
                    "cal" : [ 
                        {
                            "Datetime" : "",
                            "value" : 345.0
                        }
                    ]
                }
            ]
    }}}); 

This is my collection:

{

    "siteId" : "55",
    "thingId" : "IOT",
    "Date" : 1234545.0,
    "unique" : [ 
        {
            "controller" : 11.0,
            "module" : 10.0,
            "variable" : "curent",
            "calculation" : [ 
                {
                    "min" : "",
                    "max" : "",
                    "avg" : "",
                    "cal" : [ 
                        {
                            "Datetime" : "",
                            "value" : 345.0
                        }
                    ]
                }
            ]
        }, 
        {
            "controller" : 11.0,
            "module" : 10.0,
            "variable" : "curent",
            "calculation" : [ 
                {
                    "min" : "",
                    "max" : "c",
                    "avg" : "",
                    "cal" : [ 
                        {
                            "Datetime" : "",
                            "value" : 345.0
                        }
                    ]
                }
            ]
        }, 
        {
            "controller" : 11.0,
            "module" : 10.0,
            "variable" : "curent",
            "calculation" : [ 
                {
                    "min" : "a",
                    "max" : "c",
                    "avg" : "",
                    "cal" : [ 
                        {
                            "Datetime" : "",
                            "value" : 345.0
                        }
                    ]
                }
            ]
        }
    ],
    "thingType" : "",
    "moduleType" : 91.0,
    "enabled" : 1.0,
    "variableType" : "AI",
    "updatedTime" : NumberLong(1483209000),
    "requestedValue" : "",
    "requestedTime" : 0.0
}

This was i tried:

db.measurement.update({"siteId":"55","thingId":"IOT",
    "unique.controller":11.0,"unique.module":10.0,"unique.variable":"curent"},
    {$addToSet:{
        "unique": {
        "controller":11,
        "module": 10.0,
        "variable" : "curent",
        "calculation" : [ 
                {
                    "min" : "a",
                    "max" : "c",
                    "avg" : "",
                    "cal" : [ 
                        {
                            "Datetime" : "",
                            "value" : 345.0
                        }
                    ]
                }
            ]
    }}}); 
venkat
  • 69
  • 2
  • 9
  • Possible duplicate of [MongoDB: upsert sub-document](http://stackoverflow.com/questions/23470658/mongodb-upsert-sub-document) – s7vr Mar 21 '17 at 13:45
  • Hi venkat; in the last part of your question, you show what you tried; can you also describe what wasn't working about it? Remember to [edit] your question with any improvements or clarifications. – Vince Bowdren Mar 22 '17 at 16:34

0 Answers0