question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Update operation with aggregation pipelines

See original GitHub issue

Versions

  • NodeJS: 14.17.3
  • mongodb-memory-server-*: 6.3.3
  • mongodb: 4.4.10
  • mongoose: 6.0.12
  • system: Elementary OS 5.1.7 Hera Built on Ubuntu 18.04.6 LTS

package: Mongoose Testing (https://www.npmjs.com/package/@tsed/testing-mongoose)

What is your question?

I’m using TSED as a NodeJS framework (https://github.com/tsedio/tsed/issues/1604) and it uses a wrapper to the mongodb-memory-server in order to create automated tests.

The problem is that I’m not being able to use aggregation pipelines on updates. When running the application with MongoDB version 4.4.10 my code works, but in the test environment, it doesn’t.

Basically, I have a repository that executes a “find and replace” operation on a string property using the aggregate operator $replaceAll and then updates the current document.

async function findAndReplaceRecords(id: string, propName: string, payload: { find: string; replace: string }) {
  const doc = await this.getDocumentById(id);

  const { find, replace } = payload;

  const pipeline = [
    {
      $set: {
        records: {
          $map: {
            input: "$records",
            as: "record",
            in: {
              $mergeObjects: [
                "$$record",
                {
                  [propName]: {
                    $replaceAll: {
                      input: `$$record.${propName}`,
                      find,
                      replacement: replace
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  ];

  return doc.updateOne(pipeline);
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
hasezoeycommented, Oct 26, 2021

But that’s giving me another error

distro elementary is not greatly supported before mongodb-memory-server version 7.0.0, please try to upgrade to 7.x or set the full download URL with DOWNLOAD_URL (either via environment variables or package.json)

0reactions
github-actions[bot]commented, Jan 2, 2022

Closing Issue because it is marked as stale

Read more comments on GitHub >

github_iconTop Results From Across the Web

Updates with Aggregation Pipeline — MongoDB Manual
The following db.collection.updateMany() operation uses an aggregation pipeline to update the documents with the calculated grade average and letter grade.
Read more >
MongoDB: Modifying Documents Using Aggregation Pipelines ...
With MongoDB 4.4 and onwards, it is allowed to update a collection directly on the aggregate pipeline through the $merge stage. The magic ......
Read more >
Aggregation with update in mongoDB - Stack Overflow
With Mongo 4.2 it is now possible to do this using update with aggregation pipeline. The example 2 has example how you do...
Read more >
MongoDB: add/update fields referencing other existing ... - tsmx
Short explanation on how to add or update fields in documents referencing the values of ... Updating documents using aggregation pipelines.
Read more >
MongoDB Update Pipeline Tricks - gists · GitHub
Starting with MongoDB 4.2, you can use aggregation pipelines to update documents. Which leads to some really cool stuff.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found