Using $pull for array of arrays

I have specific structure with array of arrays

[2,{"$date": “2020-01-01T10:12:12.000Z”}, …],
[55, {"$date": “2020-03-01T10:12:12.000Z”}, …] ,
the array of “runs” contains log of procedures that where executed once and I want to delete old ones.
i.e. I need to remove all sub arrays with date less than X on 2nd place of array.

My single suggestion for this
db.myCollection.update( {}, {$pull: {runs: {1:{$lte:{$date: “2020-01-15T20:12:12.000Z”}} } } } )
should change above to something like

[55, {"$date": “2020-03-01T10:12:12.000Z”}, …] ,
but didn’t work
I currently using version 4.4.1 of mongodb
I am new for mongodb than I sure do not see something important.

any other suggestions for this case?


You don’t need 1 in your $pull just use

> db.myCollection.update({},{$pull:{runs:{$lt:ISODate('2020-03-01T10:12:12.000Z')}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

(I ran this in the shell, so I used ISODate which is equivalent to $date in extended json).
This works because operators reach inside arrays - now if you have additional elements which can also be dates, then it’s a bit more problematic (can still be done but requires a bit more complex syntax).

By the way, I would recommend storing objects in the array for this exact reason, makes it a lot easier to query for them.



But I still do not understand how it works.
If I had more than one date in the sub array, would it be both checked?

I really do not see any problem in sub arrays because from JS point of view array almost same as the object just have numeric keys.
If to any array in JS add non numeric key it automatically converted to object.
because of this here I really cannot understand why “1” cannot not be key. I just could have object like

  runs: [
    {0:55, 1:{"$date": “2020-03-01T10:12:12.000Z”}}

and from JS point of view it should be same as array.
And more obj[“1”] and obj[1] points to the same value rather obj is array or object.

This data design came to save space and time. I moved from sql and had the client that accepts such arrays
in order to not change client and simplify server (and save space and net transferred data) I moved from sql table to such array.