Skip to content

Commit

Permalink
fix: update operation in MongoDB impact all matched documents (#7803)
Browse files Browse the repository at this point in the history
mongo update operation should update all matched documents, but now it updates the first found document

fixes #7788
  • Loading branch information
SnapeEye committed Jun 26, 2021
1 parent 416cde4 commit 052014c
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/entity-manager/MongoEntityManager.ts
Expand Up @@ -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();
Expand Down
13 changes: 13 additions & 0 deletions 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;
}
65 changes: 65 additions & 0 deletions 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");
})));
});
2 changes: 1 addition & 1 deletion test/utils/test-utils.ts
Expand Up @@ -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.
Expand Down

0 comments on commit 052014c

Please sign in to comment.