Update query that handles duplicates and increment operator

Background:

  • I have an update query on collection called alerts that runs each time a "flow" is received.
  • I have an array of objects in my alert's document called blacklistedCommunication .
  • What should happen:

    When a new flow arrives, then the alerts doc is updated only if the flow's client_addr and server_addr are not already present in blacklistedCommuication . While at the same time, if we do find duplicates, it should only increment the flowCount .

    The current query:

    The below update query works to push new objects to blacklistedCommunication object if it's not present already. However, if it is indeed present, it will not update the flowCount

    How can I incorporate this logic into the query? Do I need to write a separate update query in case of duplicates?

    alerts.update({
           alertLevel: "orgLevelAlert",
           alertCategory: "blacklistedServersViolationAlert",
           alertState: "open",
           'blacklistedCommunication.client': {
               $ne: flow.netflow.client_addr
           },
           // 'blacklistedCommunication.server': {
           //     $ne: flow.netflow.server_addr
           // }  
        }, {
           $set: {
               "misc.updatedTime": new Date()
           },
           $inc: {
               flowCount: 1
           },
           $push: {
               blacklistedCommunication: {
                   client: flow.netflow.client_addr,
                   server: flow.netflow.server_addr
               }
           }
        });
    

    You can use $addToSet instead of $push . It will ensure unique {client:*,server:*} object within blacklistedCommunication and will always update flowCount :

    alerts.update({
       alertLevel: "orgLevelAlert",
       alertCategory: "blacklistedServersViolationAlert",
       alertState: "open"
    }, {
       $set: {
           "misc.updatedTime": new Date()
       },
       $inc: {
           flowCount: 1
       },
       $addToSet: {
           blacklistedCommunication: {
               client: flow.netflow.client_addr,
               server: flow.netflow.server_addr
           }
       }
    });
    
    链接地址: http://www.djcxy.com/p/61960.html

    上一篇: Python argparse可选的子

    下一篇: 更新处理重复项和增加运算符的查询