0

我有一个看起来像这样的文件:

{
    "_id" : ObjectId("5f60ffc5aefd067a9ff9345c"),
    "_class" : "com.kalsym.smart.sms.data.MSASMS",
    "number1" : NumberLong(923211105469),
    "numbers2" : [ 
        {
            "field1" : "20200915532E888",
            "number2" : NumberLong(923018565627),
            "field2" : "abcd",
            "datefield" : ISODate("2020-10-10T17:54:09.886Z")
        }, 
        {
            "field1" : "2020092570A6948",
            "number2" : NumberLong(923018565627),
            "field2" : "efgh",
            "datefield" : ISODate("2020-10-06T15:23:04.891Z")
        }, 
        {
            "field1" : "2020092570A6948",
            "number2" : NumberLong(923018565627),
            "field2" : "ijkl",
            "datefield" : ISODate("2020-10-03T15:23:04.891Z")
        }
    ],
    "optInCount" : 0
}

我想删除包含大于 2020-10-04 的日期字段值的数组索引

执行命令文件后必须像这样更新:

{
    "_id" : ObjectId("5f60ffc5aefd067a9ff9345c"),
    "_class" : "com.kalsym.smart.sms.data.MSASMS",
    "number1" : NumberLong(923211105469),
    "numbers2" : [ 
        {
            "field1" : "2020092570A6948",
            "number2" : NumberLong(923018565627),
            "field2" : "ijkl",
            "datefield" : ISODate("2020-10-03T15:23:04.891Z")
        }
    ],
    "optInCount" : 0
} 
4

1 回答 1

1

您可以使用updateMany()update(), 并$pull删除匹配的记录表单数组,

db.collection.updateMany({},
{
  $pull: {
    numbers2: { datefield: { $gt: ISODate("2020-10-04T00:00:00.000Z") } }
  }
})

操场

于 2020-10-14T09:32:29.190 回答