Skip to content

Support for the aggregation pipeline updates #4859

Closed as not planned
Closed as not planned
@sgc109

Description

@sgc109

I'd like to translate following MongoDB query into Spring Data MongoDB(and QueryDSL if needed).
Does Spring Data MongoDB support this kind of query?

I've already asked this on stackoverflow, but I couldn't get any answer.

db.user_history.findOneAndUpdate(
    { userId: 23022569 },
    [
        { 
            $replaceRoot: {
                newRoot: { 
                    $mergeObjects: [
                        { "userId": 23022569, "history": [], "autoIncrement": 0 },
                        "$$ROOT"
                    ] 
                }
            }
        },
        { 
            $set: {
                history: {
                    $cond: {
                        if: { $gt: [{ $size: "$history" }, 50] },
                        then: "$history",
                        else: { 
                            $concatArrays: [
                                "$history",
                                [{
                                    "_id": new ObjectId(),
                                    "sequence": { $add: ["$autoIncrement", 1] },
                                    "lastViewedAt": new Date(),
                                    "createdAt": new Date(),
                                }]
                            ]
                        }
                    }
                },
                autoIncrement: { $add: ["$autoIncrement", 1] }
            }
        }
    ],
    { 
        returnNewDocument: true,
        upsert: true
    }
);

A format of the user_history collection would be like this.

[
  {
    "user_id": 1,
    "history": [
      {
        "_id": new ObjectId("(random hex string)"),
        "sequence": 0,
        "lastViewedAt": new Date(),
        "createdAt": new Date(),
      }
    ],
    "auto_increment": 1,
  }
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions