简体   繁体   English

TypeORM findby MongoDB 文档的子属性

[英]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使用findfindOne等进行查找。

How do you find a matching value of a child property, like email ?如何找到子属性的匹配值,例如email Is there a better approach?有更好的方法吗?

MongoDB: Query on Nested Field MongoDB:查询嵌套字段

To specify a query condition on fields in an embedded/nested document, use dot notation.要在嵌入/嵌套文档中的字段上指定查询条件,请使用点表示法。

  • Example: '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'});

Reference:参考:


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。

Reference:参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用_bsontype属性处理来自mongoDB的文档 - how to handle document from mongoDB with _bsontype properties 如何使用 TypeORM 过滤私有属性 - how to filter private properties with TypeORM 如何将嵌套属性保存在 MongoDB 的新集合中的新文档中? - How to get nested properties to be saved in a new document in a new collection in MongoDB? MongoDB(php) - 将文档属性作为数组而不是多个属性返回 - MongoDB (php) - return document property as an array instead of multiple properties Dot运算符不提取Mongoose Document对象的子属性 - Dot operator not fetching child properties of a Mongoose Document object 在不事先知道JSON文档属性的情况下,用MongoDB和Mongoose中的另一个替换JSON文档 - Replace a JSON document with another in MongoDB & Mongoose without knowing the properties of the JSON document beforehand Mongodb - 如何在第二个文档中按键连接两个文档并将子文档作为数组合并到父文档 - Mongodb - How to Join two documents by key in second document and merge child documents as array to parent document TypeORM:使用自定义属性查询多对多 - TypeORM: Query Many-to-Many with Custom Properties NestJS + TypeORM - 连接到 mysql 和 mongodb 时出错 - NestJS + TypeORM - Error connecting to both mysql and mongodb 为什么 MongoDB 文档在进行控制台日志时会隐藏其大部分属性? - Why does a MongoDB document hide most of its properties when doing console log?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM