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 let parameters in find, update, aggregate #177

Open
aneroid opened this issue Apr 30, 2024 · 0 comments
Open

Support for let parameters in find, update, aggregate #177

aneroid opened this issue Apr 30, 2024 · 0 comments

Comments

@aneroid
Copy link

aneroid commented Apr 30, 2024

MongoDB's API supports passing in User-defined variables in the let parameter for some functions (listed below) but Mongo Playground doesn't support it. The error is:

fail to run update: write exception: write errors: [Invalid $set :: caused by :: Use of undefined variable: newFlavor]

However, using let in these commands in Mongo Compass Compass-shell works:

  1. find
  2. db.collection.update
    • update
    • updateMany supports it too but it's not documented
  3. aggregate

Is it due to let not being supported in Mongoose options?


Repro example from the docs, executed in Compass-shell:

Initialise:

db.cakeFlavors.insertMany( [
   { _id: 1, flavor: "chocolate" },
   { _id: 2, flavor: "strawberry" },
   { _id: 3, flavor: "cherry" }
] )

Using update with let params:

db.cakeFlavors.update(
   { $expr: { $eq: [ "$flavor", "$$targetFlavor" ] } },
   [ { $set: { flavor: "$$newFlavor" } } ],
   { let : { targetFlavor: "cherry", newFlavor: "orange" }, multi: true }
)

// result
{
  acknowledged: true, insertedId: null, matchedCount: 1, modifiedCount: 1, upsertedCount: 0
}

Or using updateMany with let params:

db.cakeFlavors.updateMany(
   { $expr: { $eq: [ "$flavor", "$$targetFlavor" ] } },
   [ { $set: { flavor: "$$newFlavor" } } ],
   { let : { targetFlavor: "cherry", newFlavor: "orange" } }
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant