Upsert

  • A hybrid of update and insert operation.

  • Syntax

    db.collection_name.update({<restriction query>}, {<update query>}, {"upsert": true})

  • As shown in the above query syntax, upsert option enables us to do upsert operation, by default this option is false.

        db.iot.updateOne(
            { 
                "sensor": r.sensor, "date": r.date, 
                "valcount": { "$lt": 48 } 
            },
            { 
                "$push": 
                    { "readings": 
                        { "v": r.value, "t": r.time } 
                    },
                    "$inc": 
                        { "valcount": 1, "total": r.value } 
            },
            { "upsert": true }
        )

Last updated