Skip to content

Commit

Permalink
fix: delete operation in MongoDB impact all matched documents (#7811)
Browse files Browse the repository at this point in the history
Delete operation should remove all matched documents, but now it removes the first found document

Closes: #7809
  • Loading branch information
SnapeEye committed Jun 29, 2021
1 parent db2f05d commit 0fbae53
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/entity-manager/MongoEntityManager.ts
Expand Up @@ -240,7 +240,7 @@ export class MongoEntityManager extends EntityManager {
}));

} else {
await this.deleteOne(target, this.convertMixedCriteria(this.connection.getMetadata(target), criteria));
await this.deleteMany(target, this.convertMixedCriteria(this.connection.getMetadata(target), criteria));
}

return new DeleteResult();
Expand Down
13 changes: 13 additions & 0 deletions test/github-issues/7809/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;
}
54 changes: 54 additions & 0 deletions test/github-issues/7809/issue-7809.ts
@@ -0,0 +1,54 @@
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 > #7809 MongoDB delete 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 delete all documents related to search pattern", () => Promise.all(connections.map(async connection => {
const testEntityRepository = connection.getRepository(TestEntity);

// save few documents
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.delete({ name: "Test" });

const loadedEntities = await testEntityRepository.find();

expect(loadedEntities.length).to.be.eql(1);
expect(loadedEntities[0]).to.be.instanceOf(TestEntity);
expect(loadedEntities[0]!.id).to.be.eql(thirdEntity.id);
expect(loadedEntities[0]!.name).to.be.equal("Original");
})));
});

0 comments on commit 0fbae53

Please sign in to comment.