Support for the aggregation pipeline updates
sgc109 opened this issue · 4 comments
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,
}
]
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 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.
Please have a look at the reference documentation for updates using an aggregation pipeline.
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?