[英]TypeORM findby Child Properties of a MongoDB Document
I have a MongoDB document in the following format.我有以下格式的 MongoDB 文档。 I can verify that it exists in MongoDB using Compass.
我可以使用 Compass 验证它是否存在于 MongoDB 中。 I'm using TypeORM to make the query, not MondoDB.
我正在使用 TypeORM 进行查询,而不是 MondoDB。
{
_id: 'some id'
user: {
details: {
email: "test@test.ch",
username: "testname"
},
status: 'active'
}
}
Is it possible to use TypeORM to find by, say, the email?是否可以使用 TypeORM 通过 email 进行查找?
I've tried我试过了
const emailExists = await this.userRepo.findOneBy({
user: {
details: {
email: "test@test.ch"
}
}
});
but emailExists
always returns null even though I can validate that it exists in MongoDB.但是
emailExists
总是返回 null,即使我可以验证它存在于 MongoDB 中。 I've tried other ways to find by email
using find
, findOne
, and more.我已经尝试使用其他方法通过
email
使用find
、 findOne
等进行查找。
How do you find a matching value of a child property, like email
?如何找到子属性的匹配值,例如
email
? Is there a better approach?有更好的方法吗?
To specify a query condition on fields in an embedded/nested document, use dot notation.要在嵌入/嵌套文档中的字段上指定查询条件,请使用点表示法。
'field.nestedField'
'field.nestedField'
When querying using dot notation:
使用点表示法查询时:
The field and nested field must be inside quotation marks.
字段和嵌套字段必须在引号内。
Applying in your code:在您的代码中应用:
const emailExists = await this.userRepo.findOneBy({'user.details.email': 'test@test.ch'});
Update: Looks TypeORM not work well with MongoDB, but you can try use $match
.更新:看起来 TypeORM 不适用于 MongoDB,但您可以尝试使用
$match
。
Example:例子:
$match : { 'field.nestedField': nestedField }
Applying in your code:在您的代码中应用:
this.userRepo.findOneBy({$match: { 'user.details.email': 'test@test.ch' }});
If not work maybe try to change TypeORM to Mongoose.
如果不起作用,可以尝试将 TypeORM 更改为 Mongoose。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.