简体   繁体   English

主义mongoDB仅获得一个字段

[英]Doctrine mongoDB get only one field

i have a this document: 我有这个文件:

[_id] => MongoId Object (
    [$id] => 542e6f6f10f0ed75138b4567
)
[description] => Lorem...
[lat] => 37.5184124
[lng] => 15.015836000000036
[name] => My company
[phone] => +4977665556
[photos] => Array (
    [0] => Array (
        [_id] => MongoId Object (
            [$id] => 542e6f7a10f0ed73138b4567
        )
        [image] => 06c2aeb0a8fbc03b7c77732166114a23.jpg
    )
    [1] => Array (
        [_id] => MongoId Object (
            [$id] => 542e6f7c10f0ed77138b4568
        )
        [image] => a7a428c48291137bade2fd81c842c5ab.jpg
    )
)

Now i want get only array photos with Doctrine ODM. 现在我只想用Do​​ctrine ODM获得阵列photos

I tried this query, but not working: 我试过此查询,但不起作用:

$this->createQueryBuilder('Company')
            ->select('photos')
            ->field('ID')->equals($id)
            ->getQuery()
            ->getSingleResult();

This query return all document not only photos . 此查询不仅返回所有文档的photos

Why? 为什么?

Using Builder::select() to project a specific field does not mean that the query will return that field alone. 使用Builder::select()投影特定字段并不意味着查询将单独返回该字段。 The Company's query builder will always return Company objects. 公司的查询生成器将始终返回公司对象。 In your case, getSingleResult() has the query return a single object instead of an iterable cursor of Company objects. 在您的情况下, getSingleResult()使查询返回单个对象,而不是Company对象的可迭代游标。 The select() projection means that the MongoDB query will only return the _id and photos fields, which will then be hydrated into a Company object. select()投影意味着MongoDB查询将仅返回_idphotos字段,然后将其合并为Company对象。

As a result, you'll find that Company::getName() probably returns null , while Company::getPhotos() probably returns a PersistentCollection of Photo objects. 结果,您会发现Company::getName()可能返回null ,而Company::getPhotos()可能返回Photo对象的PersistentCollection It's unclear to me from your example if those are referenced or simply embedded documents with an _id field. 从您的示例中,我不清楚这是引用的引用文件还是带_id字段的嵌入文档。

By default, ODM hydrates query results. 默认情况下,ODM合并查询结果。 This can be problematic when you're using projections to exclude fields, as any further assignments to non-hydrated fields might be picked up as a change and persisted back to the database. 当您使用投影来排除字段时,这可能会出现问题,因为对非水合字段的任何其他分配都可能会作为更改而被提取并保存回数据库。 If you're rather obtain the raw data, you can disable hydration with via Builder::hydrate() , which takes a boolean. 如果您想获取原始数据,则可以通过Builder::hydrate()禁用布尔值化,从而实现水合作用。

On a side note, I'd suggest you use 'id' or '_id' instead of 'ID' in your field() query. 附带一提,我建议您在field()查询中使用'id''_id'而不是'ID' I'm not sure if ODM does a case-insensitive string comparison when it looks up mapped fields -- unless of course your PHP property is $ID , in which case you can ignore this suggestion. 我不确定ODM在查找映射字段时是否进行不区分大小写的字符串比较-除非您的PHP属性当然是$ID ,在这种情况下您可以忽略此建议。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM