Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for the aggregation pipeline updates #4859

Closed
sgc109 opened this issue Dec 19, 2024 · 4 comments
Closed

Support for the aggregation pipeline updates #4859

sgc109 opened this issue Dec 19, 2024 · 4 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@sgc109
Copy link

sgc109 commented Dec 19, 2024

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,
  }
]
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 19, 2024
@sgc109 sgc109 changed the title Support for this aggregation pipeline updates Support for the aggregation pipeline updates Dec 19, 2024
@christophstrobl
Copy link
Member

Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. We prefer to use GitHub issues only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.

@christophstrobl christophstrobl closed this as not planned Won't fix, can't repro, duplicate, stale Dec 19, 2024
@christophstrobl christophstrobl added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 19, 2024
@sgc109
Copy link
Author

sgc109 commented Dec 19, 2024

@christophstrobl Thank you for reply! Actually, I'm not asking how I can translate this query to Spring Data MongoDB code, but I just wanted to suggest future improvements if Spring Data MongoDB doesn't support this functionality at the moment.

@christophstrobl
Copy link
Member

Please have a look at the reference documentation for updates using an aggregation pipeline.

@sgc109
Copy link
Author

sgc109 commented Dec 19, 2024

I've actually seen that part of the documentation. According to it, The update in a aggregation pipeline can consist of the following stages: set, unset, replaceWith, but not replaceRoot. So, I'd like to suggest future support for replaceRoot operation in aggregation pipeline updates. Additionally, it doesn't seem like I can specify the filtering condition(it is just { } for the first param of translated javascript code) and get the result of the specific method(just update, not findOneAndUpdate) seeing the example on the documentation. Would it make sense to suggest these features?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants