簡體   English   中英

如何根據相關文檔查詢MongoDB集合(在教義中)

[英]How do I query MongoDB collection based on related documents (in Doctrine)

我的Doctrine ODM項目中有一些相關的收藏……

# Contract.mongodb.yml
Project\Contract\Domain\Contract:
  type:             document
  repositoryClass:  Project\SymfonyBundle\ContractBundle\Repository\ContractRepository
  collection:       Contracts
  fields:
    id:
      type:     id
      id:       true
      strategy: UUID
    slug:
      type:   string
      length: 128
      unique: true
      gedmo:
        slug:
          separator:  -
          style:      default
          fields:
            - refNo
            - name
    name:
      type: string
    refNo:
      type:   string
    purpose:
      type: string
    budgetAmount:
      type: int
    budgetCurrency:
      type:   string
      length: 3
    startDate:
      type: date_immutable
    endDate:
      type: date_immutable
    provider:
      type: string
  referenceOne:
    provider:
      targetDocument: Project\Contract\Domain\Provider
      cascade:
        - persist
        - merge
        - detach
        - refresh
  referenceMany:
    reports:
      targetDocument: Project\Report\Domain\Report
      cascade:
        - all

# Provider.mongodb.yml
Project\Contract\Domain\Provider:
  type:             document
  repositoryClass:  Project\SymfonyBundle\ContractBundle\Repository\ProviderRepository
  collection:       Providers
  fields:
    id:
      type:     id
      id:       true
      strategy: UUID
    slug:
      type:   string
      length: 128
      unique: true
      gedmo:
        slug:
          separator:  -
          style:      default
          fields:
            - name
    name:
      type:   string
      unique: true
  referenceMany:
    users:
      targetDocument: Project\User\Domain\User
      cascade:        []

# User.mongodb.yml
Project\User\Domain\User:
  type:             document
  repositoryClass:  Project\SymfonyBundle\UserBundle\Repository\UserRepository
  collection:       Users
  fields:
    id:
      type:     id
      id:       true
      strategy: UUID

我想做的是獲取給定用戶的合同,但是我不知道如何根據用戶查詢合同集合。 我需要查詢兩個嗎? 1獲取用戶的提供者,然后第二秒查詢鏈接到提供者之一的合同?

如果您能夠建議我如何在控制台以及Doctrine中做到這一點,我將不勝感激。

預先感謝您的幫助:o)

您可以使用聚合管道並使用$lookup運算符來加入文檔,請參見-https://docs.mongodb.com/v3.2/reference/operator/aggregation/lookup/

但是,如果這很常見,我會考慮重新建模您的文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM