0

Here is an example of my data:

{
    "_id" : ObjectId("6144f74c066fee820d28b34f"),
    "Login" : ISODate("2021-09-16T21:09:00.000Z"),
    "DiaryNotes" : [ 
        {
            "Message" : "Metus tincidunt ultricies.",
            "Timestamp" : ISODate("1922-07-29T01:42:00.000Z")
        }, 
        {
            "Message" : "Ligula, eleifend ultrices, fames in, est aliquam ex congue.",
            "Timestamp" : ISODate("2022-01-09T12:48:00.000Z")
        }
    ],
}

The following query works for Mongo 4.2+:

db.getCollection('Surveys').updateMany({},
[
  {
    $set: {
      DiaryNotes: {
        $filter: {
          input: "$DiaryNotes",
          cond: { $lt: [ "$Login", "$$this.Timestamp" ] }
        }
      }
    }
  }
])

But if this query is executed against a CosmosDB service using Mongo 4.0 API I get an error: "Expected type object but found array." I think this is because Mongo 4.0 does not support update aggregation pipelines.

Can this query be refactored in a way that supports Mongo 4.0?

4

1 回答 1

0

最后我使用了一个简单的 forEach 循环。效率低下,但达到了我所需要的。

db.getCollection('Surveys').find().forEach(function (doc) {

    var validDiaryNotes = doc.DiaryNotes.filter(d => d.Timestamp > doc.Login);
    var validSMSs = doc.SMSs.filter(s => s.Timestamp > doc.Login);

    db.Surveys.update({ _id: doc._id }, { $set: { "DiaryNotes": validDiaryNotes, "SMSs": validSMSs } });
}
);
于 2022-01-04T03:29:44.763 回答