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

Adjust replaceWith type pipeline stage #12535

Conversation

FabioCingottini
Copy link

@FabioCingottini FabioCingottini commented Oct 6, 2022

Summary

For how the $replaceWith pipeline stage type was written, it wasn't possible to replace the document by using expressions.

Examples

Things like the following were not possible, now they're.

db.sales.insertMany([
  { "_id" : 1, "item" : "butter", "price" : 10, "quantity": 2, date: ISODate("2019-03-01T08:00:00Z"), status: "C" },
  { "_id" : 2, "item" : "cream", "price" : 20, "quantity": 1, date: ISODate("2019-03-01T09:00:00Z"), status: "A" },
  { "_id" : 3, "item" : "jam", "price" : 5, "quantity": 10, date: ISODate("2019-03-15T09:00:00Z"), status: "C" },
  { "_id" : 4, "item" : "muffins", "price" : 5, "quantity": 10, date: ISODate("2019-03-15T09:00:00Z"), status: "C" }
])
SalesModel.aggregate([
    { $match: { status: "C" } },
    { 
        $replaceWith: {
            _id: "$_id",
            item: "$item",
            amount: {
                $multiply: [ "$price", "$quantity"]
            },
            status: "Complete", asofDate: "$NOW" 
        }
    }
])

replaceWith pipeline stage was not accepting a field
as a parameter. This was causing ts error when
users where trying to call aggregate method with
replaceWith stage in unusual ways
Copy link
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add tests to ./test/types to avoid a future regression?
Thanks @FabioCingottini

@hasezoey hasezoey added the typescript Types or Types-test related issue / Pull Request label Oct 7, 2022
Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍

@vkarpov15 vkarpov15 added this to the 6.6.6 milestone Oct 9, 2022
@vkarpov15 vkarpov15 merged commit 67eba85 into Automattic:master Oct 9, 2022
@feelgood-interface
Copy link

feelgood-interface commented Oct 18, 2022

Shouldn't string be allowed too?

db.collection.aggregate([
{ $replaceWith: "$name" }
])

@vkarpov15
Copy link
Collaborator

@feelgood-interface that was added in 616ce5d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants