I have the following documents:
- A
User . - The attached document containing the link (see the following document)
- a
Site document
Each user has an array of embedded documents inside, each of which represents the element that he follows - a site, a Twitter account, with the ability to mark categories of interest to them. Each embedded document has a link to a third document - a Site document (or Twitter document, etc.).
Question: using ODM MongoDB, how can I get documents of all users who decide to follow this site using the identifier of this site?
(see below (after files) how to do this in the mongodb shell)
User.php
<?php use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; class User { protected $id; protected $follows; }
SiteFollow.php
<?php use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; class SiteFollow { protected $interestingCategories; protected $siteItem; }
Site.php
<?php use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; class Site { protected $id; protected $name; protected $url; }
An example for a custom document in the mongo shell:
db.User.findOne() { "_id": ObjectId("123"), "follows": [ { "interestingCategories": [ "PHP" ] "siteItem" : ObjectId("OBJECT_ID_OF_SITE_DOCUMENT"), "_doctrine_class_name" : "site" } ] }
Mongo shell to get all users following a specific site:
db.User.find({"follows.siteItem": ObjectId("OBJECT_ID_OF_SITE_DOCUMENT")})
source share