Skip to content

Commit c3d7fbf

Browse files
committed
fix(mongodb): add missing findBy method to MongoEntityManager (#10264)
The findBy() method was missing in MongoEntityManager, causing it to inherit the SQL-based implementation from EntityManager which uses QueryBuilder. Since MongoDB does not support QueryBuilder, this resulted in a Query Builder is not supported by MongoDB error. This fix adds the findBy() method override to use MongoDB's native cursor API instead, following the same pattern as existing methods like findAndCountBy() and findOneBy(). Changes: - Add findBy() method to MongoEntityManager - Add test case in mongo-repository.test.ts - Remove incorrectly placed mongo-query-builder test directory Fixes #10264
1 parent ec3ea10 commit c3d7fbf

2 files changed

Lines changed: 71 additions & 28 deletions

File tree

‎src/entity-manager/MongoEntityManager.ts‎

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,46 @@ import { DeleteResult } from "../query-builder/result/DeleteResult"
1515
import { EntityMetadata } from "../metadata/EntityMetadata"
1616

1717
import {
18-
BulkWriteResult,
19-
AggregationCursor,
20-
Collection,
21-
FindCursor,
22-
Document,
2318
AggregateOptions,
19+
AggregationCursor,
2420
AnyBulkWriteOperation,
2521
BulkWriteOptions,
26-
Filter,
22+
BulkWriteResult,
23+
ChangeStream,
24+
ChangeStreamOptions,
25+
Collection,
26+
CollStats,
27+
CollStatsOptions,
28+
CommandOperationOptions,
29+
CountDocumentsOptions,
2730
CountOptions,
28-
IndexSpecification,
2931
CreateIndexesOptions,
30-
IndexDescription,
31-
DeleteResult as DeleteResultMongoDb,
3232
DeleteOptions,
33-
CommandOperationOptions,
33+
DeleteResult as DeleteResultMongoDb,
34+
Document,
35+
Filter,
36+
FilterOperators,
37+
FindCursor,
3438
FindOneAndDeleteOptions,
3539
FindOneAndReplaceOptions,
36-
UpdateFilter,
3740
FindOneAndUpdateOptions,
38-
RenameOptions,
39-
ReplaceOptions,
40-
UpdateResult as UpdateResultMongoDb,
41-
CollStats,
42-
CollStatsOptions,
43-
ChangeStreamOptions,
44-
ChangeStream,
45-
UpdateOptions,
46-
ListIndexesOptions,
47-
ListIndexesCursor,
48-
OptionalId,
41+
IndexDescription,
42+
IndexInformationOptions,
43+
IndexSpecification,
44+
InsertManyResult,
4945
InsertOneOptions,
5046
InsertOneResult,
51-
InsertManyResult,
52-
UnorderedBulkOperation,
53-
OrderedBulkOperation,
54-
IndexInformationOptions,
47+
ListIndexesCursor,
48+
ListIndexesOptions,
5549
ObjectId,
56-
FilterOperators,
57-
CountDocumentsOptions,
50+
OptionalId,
51+
OrderedBulkOperation,
52+
RenameOptions,
53+
ReplaceOptions,
54+
UnorderedBulkOperation,
55+
UpdateFilter,
56+
UpdateOptions,
57+
UpdateResult as UpdateResultMongoDb,
5858
} from "../driver/mongodb/typings"
5959
import { DataSource } from "../data-source/DataSource"
6060
import { MongoFindManyOptions } from "../find-options/mongodb/MongoFindManyOptions"
@@ -161,6 +161,16 @@ export class MongoEntityManager extends EntityManager {
161161
return this.executeFindAndCount(entityClassOrName, where)
162162
}
163163

164+
/**
165+
* Finds entities that match given WHERE conditions.
166+
*/
167+
async findBy<Entity>(
168+
entityClassOrName: EntityTarget<Entity>,
169+
where: any,
170+
): Promise<Entity[]> {
171+
return this.executeFind(entityClassOrName, where)
172+
}
173+
164174
/**
165175
* Finds entities by ids.
166176
* Optionally find options can be applied.

‎test/functional/mongodb/basic/mongo-repository/mongo-repository.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,39 @@ describe("mongodb > MongoRepository", () => {
265265
))
266266
})
267267
})
268+
269+
it("should be able to use findBy method", () =>
270+
Promise.all(
271+
connections.map(async (connection) => {
272+
const postRepository = connection.getMongoRepository(Post)
273+
274+
// save few posts
275+
const firstPost = new Post()
276+
firstPost.title = "Post #1"
277+
firstPost.text = "Everything about post #1"
278+
await postRepository.save(firstPost)
279+
280+
const secondPost = new Post()
281+
secondPost.title = "Post #1"
282+
secondPost.text = "Everything about post #2"
283+
await postRepository.save(secondPost)
284+
285+
const thirdPost = new Post()
286+
thirdPost.title = "Post #2"
287+
thirdPost.text = "Everything about post #3"
288+
await postRepository.save(thirdPost)
289+
290+
const loadedPosts = await postRepository.findBy({
291+
title: "Post #1",
292+
})
293+
294+
expect(loadedPosts).to.have.length(2)
295+
expect(loadedPosts[0]).to.be.instanceOf(Post)
296+
expect(loadedPosts[1]).to.be.instanceOf(Post)
297+
expect(loadedPosts[0].title).to.eql("Post #1")
298+
expect(loadedPosts[1].title).to.eql("Post #1")
299+
}),
300+
))
268301
})
269302

270303
async function seedPosts(postRepository: MongoRepository<PostWithDeleted>) {

0 commit comments

Comments
 (0)