$merge, $match and $update in one aggregate query

Viewed 26

I have data in a collection ex:"jobs". I am trying to copy specific data from "jobs" after every 2 hours to a new collection (which may not exist initially) and also add a new key to the copied data. I have been trying with this query to copy the data:

db.getCollection("jobs").aggregate([{ $match: { "job_name": "UploadFile", "created_datetime" : {"$gte":"2021-08-18 12:00:00"} } },{"$merge":{into: {coll : "reports"}}}])

But after this, the count in "reports" collection is 0. Also, how can I update the documents (with an extract key "report_name") without using an extra updateMany() query?

The data in jobs collection is as shown:

{
    "_id" : ObjectId("60fa8e8283dc22799134dc6f"),
    "job_id" : "408a5654-9a89-4c15-82b4-b0dc894b19d7",
    "job_name" : "UploadFile",
    "data" : {
        "path" : "share://LOCALNAS/Screenshot from 2021-07-23 10-34-34.png",
        "file_name" : "Screenshot from 2021-07-23 10-34-34.png",
        "parent_path" : "share://LOCALNAS",
        "size" : 97710,
        "md5sum" : "",
        "file_uid" : "c4411f10-a745-48d0-a55d-164707b7d6c2",
        "version_id" : "c3dfd31a-80ba-4de0-9115-2d9b778bcf02",
        "session_id" : "c4411f10-a745-48d0-a55d-164707b7d6c2",
        "resource_name" : "Screenshot from 2021-07-23 10-34-34.png",
        "metadata" : {
            "metadata" : {
                "description" : "",
                "tag_ids" : [ ]
            },
            "category_id" : "60eed9ea33c690a0dfc89b41",
            "custom_metadata" : [ ]
        },
        "upload_token" : "upload_token_c5043927484e",
        "upload_url" : "/mnt/share_LOCALNAS",
        "vfs_action_handler_id" : "91be4282a9ad5067642cdadb75278230",
        "element_type" : "file"
    },
    "user_id" : "60f6c507d4ba6ee28aee5723",
    "node_id" : "syeda",
    "state" : "COMPLETED",
    "priority" : 2,
    "resource_name" : "Screenshot from 2021-07-23 10-34-34.png",
    "group_id" : "upload_group_0babf8b7ce0b",
    "status_info" : {
        "progress" : 100,
        "status_msg" : "Upload Completed."
    },
    "error_code" : "",
    "error_message" : "",
    "created_datetime" : ISODate("2021-07-23T15:10:18.506Z"),
    "modified_datetime" : ISODate("2021-07-23T15:10:18.506Z"),
    "schema_version" : "1.0.0",
}
1 Answers

Your $match stage contains a condition which takes created_datetime as string while in your sample data it is an ISODate. Such condtion won't return any document, try:

{
    $match: {
        "job_name": "UploadFile",
        "created_datetime": {
            "$gte": ISODate("2021-07-01T12:00:00.000Z")
        }
    }
}

Mongo Playground

Related