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

Update in nested array based on Date queries may not work properly

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Duplicate
    • Affects Version/s: 3.6 Required
    • Fix Version/s: None
    • Component/s: Internal Client
    • Labels:
      None
    • Operating System:
      ALL
    • Steps To Reproduce:
      Hide

      Create collection "test", and insert one document into it:

      {
      {
          "_id" : NumberLong(1),
          "pointsBalances" : [ 
              {
                  "period" : "2020",
                  "startDate" : ISODate("2020-01-01T00:00:00.000Z"),
                  "endDate" : ISODate("2020-12-31T23:59:59.999Z"),
                  "value" : 0
              }, 
              {
                  "period" : "2019",
                  "startDate" : ISODate("2019-01-01T00:00:00.000Z"),
                  "endDate" : ISODate("2019-12-31T23:59:59.999Z"),
                  "value" : 0
              }
          ]
      }
      

      Then please perform update - it should work properly:

      db.getCollection('test').update(
        {$and: [ {'_id': 1}, {"pointsBalances.period": '2019'}]},
        {$set: { "pointsBalances.$.value": 100 } }
      )
      

      Now lets try to update based on dates:

      db.getCollection('test').update(
      { $and: [ {'_id': 1}, {"pointsBalances.startDate": {$lte: ISODate("2019-05-01T00:00:00.000Z")}}, {"pointsBalances.endDate": {$gte: ISODate("2019-05-01T00:00:00.000Z")}}  ]},
        {$set: { "pointsBalances.$.value": 200 } }
      )
      

      Second code updates wrong array element.
      Please notice that this behavior only occurs if points balances are not sorted. So in this case it will work properly:

      {
          "_id" : NumberLong(1),
          "pointsBalances" : [ 
              {
                  "period" : "2019",
                  "startDate" : ISODate("2019-01-01T00:00:00.000Z"),
                  "endDate" : ISODate("2019-12-31T23:59:59.999Z"),
                  "value" : 0
              }, 
              {
                  "period" : "2020",
                  "startDate" : ISODate("2020-01-01T00:00:00.000Z"),
                  "endDate" : ISODate("2020-12-31T23:59:59.999Z"),
                  "value" : 0
              }
          ]
      }
      

      Show
      Create collection "test", and insert one document into it: { { "_id" : NumberLong(1), "pointsBalances" : [ { "period" : "2020" , "startDate" : ISODate( "2020-01-01T00:00:00.000Z" ), "endDate" : ISODate( "2020-12-31T23:59:59.999Z" ), "value" : 0 }, { "period" : "2019" , "startDate" : ISODate( "2019-01-01T00:00:00.000Z" ), "endDate" : ISODate( "2019-12-31T23:59:59.999Z" ), "value" : 0 } ] } Then please perform update - it should work properly: db.getCollection( 'test' ).update( {$and: [ { '_id' : 1}, { "pointsBalances.period" : '2019' }]}, {$set: { "pointsBalances.$.value" : 100 } } ) Now lets try to update based on dates: db.getCollection( 'test' ).update( { $and: [ { '_id' : 1}, { "pointsBalances.startDate" : {$lte: ISODate( "2019-05-01T00:00:00.000Z" )}}, { "pointsBalances.endDate" : {$gte: ISODate( "2019-05-01T00:00:00.000Z" )}} ]}, {$set: { "pointsBalances.$.value" : 200 } } ) Second code updates wrong array element. Please notice that this behavior only occurs if points balances are not sorted. So in this case it will work properly: { "_id" : NumberLong(1), "pointsBalances" : [ { "period" : "2019" , "startDate" : ISODate( "2019-01-01T00:00:00.000Z" ), "endDate" : ISODate( "2019-12-31T23:59:59.999Z" ), "value" : 0 }, { "period" : "2020" , "startDate" : ISODate( "2020-01-01T00:00:00.000Z" ), "endDate" : ISODate( "2020-12-31T23:59:59.999Z" ), "value" : 0 } ] }

      Description

      Update in nested array based on Date queries may notwork properly.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              eric.sedor Eric Sedor
              Reporter:
              michal_kotra Michal Kotra
              Participants:
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: