简体   繁体   English

Doctrine2-如何在MongoDB中查询引用的文档

[英]Doctrine2 - How to query for referenced documents in MongoDB

I have two collections, categories and jobs , the jobs collection is referenced inside categories . 我有两个集合, categoriesjobscategories引用了jobs集合。

Now i have in CategoryRepository a method called getWithActiveJobs() , This method returns all categories with referenced jobs that only not expired and activated . 现在,我在CategoryRepository有一个名为getWithActiveJobs()方法,该方法返回所有带有未过期和未激活的引用作业的类别。

The problem is when i run this method nothing get returned. 问题是当我运行此方法时,什么也没有返回。 I'm new to MongoDB, So please tell me how to query for jobs inside CategoryRepository ? 我是MongoDB的新手,所以请告诉我如何在CategoryRepository内查询jobs

Here's my collections (setters and getters and other methods are excluded): 这是我的收藏集(不包括setter和getter以及其他方法):

Category.php : Category.php

namespace Ibw\JobeetBundle\Document;

use Ibw\JobeetBundle\Utils\Jobeet;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\Document(collection="categories", repositoryClass="Ibw\JobeetBundle\Repository\CategoryRepository")
 */
class Category
{

    /**
     * @MongoDB\Id(strategy="AUTO")
     */
    protected $id;

    /**
     * @MongoDB\String
     */
    protected $name;

    /**
     * @MongoDB\ReferenceMany(targetDocument="Job")
     */
    protected $jobs = array();

    /**
     * @MongoDB\ReferenceMany(targetDocument="Affiliate")
     */
    protected $affiliates = array();

    /**
     * @MongoDB\String
     */
    protected $slug;
}

Job.php : Job.php

namespace Ibw\JobeetBundle\Document;

use Ibw\JobeetBundle\Utils\Jobeet;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\Document(collection="jobs", repositoryClass="Ibw\JobeetBundle\Repository\JobRepository")
 * @Assert\GroupSequence({"Form", "Job"})
 */
class Job
{
    /**
     * @MongoDB\Id(strategy="AUTO")
     */
    protected $id;

    /**
         * @MongoDB\String
         * @Assert\NotBlank(groups={"Form"})
         * @Assert\Choice(callback="getTypeValues", groups={"Form"})
     */
    protected $type;

    /**
         * @MongoDB\String
         * @Assert\NotBlank(groups={"Form"})
     */
    protected $company;

    /**
         * @MongoDB\String
     */
    protected $logo;

        /**
         * @Assert\Image(groups={"Form"})
         */
        protected $file;

    /**
         * @MongoDB\String
         * @Assert\Url(groups={"Form"})
     */
    protected $url;

    /**
         * @MongoDB\String
         * @Assert\NotBlank(groups={"Form"})
     */
    protected $position;

    /**
         * @MongoDB\String
         * @Assert\NotBlank(groups={"Form"})
     */
    protected $location;

    /**
         * @MongoDB\String
         * @Assert\NotBlank(groups={"Form"})
     */
    protected $description;

    /**
         * @MongoDB\String
         * @Assert\NotBlank(groups={"Form"})
     */
    protected $how_to_apply;

    /**
         * @MongoDB\String
         * @Assert\NotBlank()
     */
    protected $token;

    /**
         * @MongoDB\Boolean
     */
    protected $is_public;

    /**
         * @MongoDB\Boolean
     */
    protected $is_activated;

    /**
         * @MongoDB\String
         * @Assert\NotBlank(groups={"Form"})
         * @Assert\Email(groups={"Form"})
     */
    protected $email;

    /**
         * @MongoDB\Date
     */
    protected $expires_at;

    /**
         * @MongoDB\Date
     */
    protected $created_at;

    /**
         * @MongoDB\Date
     */
    protected $updated_at;

    /**
         * @MongoDB\ReferenceOne(targetDocument="Category", cascade={"persist"})
         * @Assert\NotBlank(groups={"Form"})
     */
    protected $category;
}

And here's the CategoryRepository.php method: 这是CategoryRepository.php方法:

public function getWithActiveJobs($limit = null)
{
    $qb = $this->createQueryBuilder()
                ->field('jobs')->prime(true)
                ->field('jobs.expires_at')->gt(date('Y-m-d H:i:s', time()))
                ->field('jobs.is_activated')->equals(true)
                ->sort('jobs.expires_at', 'DESC');

    if($limit)
    {
        $qb->limit($limit);
    }

    return $qb->getQuery()->execute();
}

Here's MongoDB categories collection: 这是MongoDB categories集合:

> db.categories.find().pretty()
{
    "_id" : ObjectId("527b884610fedf400d8b4589"),
    "name" : "Design",
    "slug" : "design"
}
{
    "_id" : ObjectId("527b884610fedf400d8b458c"),
    "name" : "Administrator",
    "slug" : "administrator"
}
{
    "_id" : ObjectId("527b884610fedf400d8b458a"),
    "jobs" : [
        DBRef("jobs", ObjectId("527b884610fedf400d8b4587"))
    ],
    "name" : "Programming",
    "slug" : "programming"
}
{
    "_id" : ObjectId("527b884610fedf400d8b458b"),
    "jobs" : [
        DBRef("jobs", ObjectId("527b884610fedf400d8b4588"))
    ],
    "name" : "Manager",
    "slug" : "manager"
}

This is a common error when starting with mongodb: 从mongodb开始时,这是一个常见错误:

you can't do JOIN 你不能JOIN

The prime attribute will fetch the Jobs documents, but only after the query got executed, and won't help. prime属性将获取Jobs文档,但仅在查询执行后才会获取,将无济于事。

You can try a different approach: 您可以尝试其他方法:

Use a distinct query and fetch all the category IDS of the active jobs: 使用不同的查询并获取活动作业的所有类别IDS:

db.Job.distinct( 'category.$id', { is_activated: true } )

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

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