Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-14912

Need alternative upsert for time series collection

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.4.10
    • Component/s: None
    • None
    • ALL

      I would like to either insert or update a record in a time series collection and do this in one pass with a update() command with the $setOnInsert and $inc commands.

      I would like to avoid querying to find out if the record exists and then doing an insert if it does not exist or an update increment if the record does exist.

      For example: This does not work:

      db.EventRecord.update(
      { "appKey" : "apRiBsgUCm" , "name" : "ApplicationOpened" , "type" : "application" },
      { 
        "$setOnInsert" : { "appKey" : "apRiBsgUCm" ,  "type" : "application" , "name" : "ApplicationOpened" , "totalEvents" : 0 ,
                         "hours" : {"18" : { "totalHr" : 0 , "0" : 0 , "5" : 0 , "10" : 0 , "15" : 0 , "20" : 0 , "25" : 0 , "30" : 0 , "35" : 0 , "40" : 0 , "45" : 0 , "50" : 0 , "55" : 0 } } },
        "$inc" : { "totalEvents" : 1 , "hours.18.0" : 1 ,    "hours.18.totalHr" : 1}
      },
      {"upsert":true, "multi":false}
      )
      // get error: Field name duplication not allowed with modifiers
      

      But this does.

      db.find({ "appKey" : "apRiBsgUCm" , "name" : "ApplicationOpened" , "type" : "application" }) 
      

      Then if record exists:

      db.EventRecord.update(
      { "appKey" : "apRiBsgUCm" , "name" : "ApplicationOpened" , "type" : "application" },
      { 
      
        "$inc" : { "totalEvents" : 1 , "hours.18.0" : 1 ,    "hours.18.totalHr" : 1}
      },
      {"upsert":true, "multi":false}
      )
      

      Else if the record doesn't exist, then do an insert.

      If I do the query and then the if test, for every single event, I have to access the database twice - one to query the document to see if it exists and then again to insert or update the record.

      What is a better strategy? How can I access the database once to upsert the document in this time series collection. This collection is being updated millions of times a day. I'd like to increase my performance.

            Assignee:
            Unassigned Unassigned
            Reporter:
            lagrwilson Lucille Wilson
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: