Skip to content

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

@aneroid

Description

@aneroid

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" } }
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions