From 40a2ecba8a8c7e721bf66c330093cefe839ef2df Mon Sep 17 00:00:00 2001 From: Pavlo Mykhailenko Date: Fri, 25 Jun 2021 12:32:23 +0300 Subject: [PATCH] fix: update operation in MongoDB impact all matched documents Update operation should update all matched documents, but now it updates the first found document Closes: #7788 --- src/entity-manager/MongoEntityManager.ts | 2 +- test/github-issues/7788/entity/test.entity.ts | 13 ++++ test/github-issues/7788/issue-7788.ts | 65 +++++++++++++++++++ test/utils/test-utils.ts | 2 +- 4 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 test/github-issues/7788/entity/test.entity.ts create mode 100644 test/github-issues/7788/issue-7788.ts diff --git a/src/entity-manager/MongoEntityManager.ts b/src/entity-manager/MongoEntityManager.ts index a23be20b33..d6981a96aa 100644 --- a/src/entity-manager/MongoEntityManager.ts +++ b/src/entity-manager/MongoEntityManager.ts @@ -221,7 +221,7 @@ export class MongoEntityManager extends EntityManager { } else { const metadata = this.connection.getMetadata(target); - await this.updateOne(target, this.convertMixedCriteria(metadata, criteria), { $set: partialEntity }); + await this.updateMany(target, this.convertMixedCriteria(metadata, criteria), { $set: partialEntity }); } return new UpdateResult(); diff --git a/test/github-issues/7788/entity/test.entity.ts b/test/github-issues/7788/entity/test.entity.ts new file mode 100644 index 0000000000..75dba89ece --- /dev/null +++ b/test/github-issues/7788/entity/test.entity.ts @@ -0,0 +1,13 @@ +import {Column, Entity, ObjectIdColumn, PrimaryColumn} from "../../../../src"; + +@Entity("test") +export class TestEntity { + @ObjectIdColumn() + _id: string; + + @PrimaryColumn() + id: string; + + @Column() + name: string; +} diff --git a/test/github-issues/7788/issue-7788.ts b/test/github-issues/7788/issue-7788.ts new file mode 100644 index 0000000000..f7a17f2a1e --- /dev/null +++ b/test/github-issues/7788/issue-7788.ts @@ -0,0 +1,65 @@ +import "reflect-metadata"; +import { expect } from "chai"; +import { Connection } from "../../../src"; +import { closeTestingConnections, createTestingConnections, reloadTestingDatabases } from "../../utils/test-utils"; +import { TestEntity } from "./entity/test.entity"; + +describe("github issues > #7788 MongoDB update make changes only to first matched document", () => { + + let connections: Connection[]; + before(async () => { + connections = await createTestingConnections({ + enabledDrivers: ["mongodb"], + entities: [TestEntity], + schemaCreate: false, + dropSchema: true + }); + }); + beforeEach(() => reloadTestingDatabases(connections)); + after(() => closeTestingConnections(connections)); + + it("should update all documents related to search pattern", () => Promise.all(connections.map(async connection => { + const testEntityRepository = connection.getMongoRepository(TestEntity); + + // save few posts + const firstEntity = new TestEntity(); + firstEntity.id = "1"; + firstEntity.name = "Test"; + await testEntityRepository.save(firstEntity); + + const secondEntity = new TestEntity(); + secondEntity.id = "2"; + secondEntity.name = "Test"; + await testEntityRepository.save(secondEntity); + + const thirdEntity = new TestEntity(); + thirdEntity.id = "3"; + thirdEntity.name = "Original"; + await testEntityRepository.save(thirdEntity); + + const fourthEntity = new TestEntity(); + fourthEntity.id = "4"; + fourthEntity.name = "Test"; + await testEntityRepository.save(fourthEntity); + + await testEntityRepository.update({ name: "Test" }, { name: "Updated" }); + + const loadedEntities = await testEntityRepository.find(); + + expect(loadedEntities[0]).to.be.instanceOf(TestEntity); + expect(loadedEntities[0]!.id).to.be.eql(firstEntity.id); + expect(loadedEntities[0]!.name).to.be.equal("Updated"); + + expect(loadedEntities[1]).to.be.instanceOf(TestEntity); + expect(loadedEntities[1]!.id).to.be.eql(secondEntity.id); + expect(loadedEntities[1]!.name).to.be.equal("Updated"); + + expect(loadedEntities[2]).to.be.instanceOf(TestEntity); + expect(loadedEntities[2]!.id).to.be.eql(thirdEntity.id); + expect(loadedEntities[2]!.name).to.be.equal("Original"); + + expect(loadedEntities[3]).to.be.instanceOf(TestEntity); + expect(loadedEntities[3]!.id).to.be.eql(fourthEntity.id); + expect(loadedEntities[3]!.name).to.be.equal("Updated"); + }))); +}); diff --git a/test/utils/test-utils.ts b/test/utils/test-utils.ts index 37bb00d9fb..6aab2d643b 100644 --- a/test/utils/test-utils.ts +++ b/test/utils/test-utils.ts @@ -8,7 +8,7 @@ import {createConnections} from "../../src/index"; import {NamingStrategyInterface} from "../../src/naming-strategy/NamingStrategyInterface"; import {QueryResultCache} from "../../src/cache/QueryResultCache"; import {Logger} from "../../src/logger/Logger"; -import {CockroachDriver} from "../../src/driver/cockroachdb/CockroachDriver.js"; +import {CockroachDriver} from "../../src/driver/cockroachdb/CockroachDriver"; /** * Interface in which data is stored in ormconfig.json of the project.