質問

I am trying to retrieve the data from a collection by matching its subdocument's id but its giving me no result. here is the my code

$usrcollection = $db->users;    
$where = array('following'=> array('followerid' => '52a97985f770dfdc04000000', 'type' => "'user'"));
$usrcursor = $usrcollection->find($where);

i have tried using $elemMatch as well but the result is same

$where = array('following' => array(
            '$elemMatch' => array(          
            'followerid' => '52a97985f770dfdc04000000',
            'type' => "'user'")
            )       
        );

Anybody please help me to resolve this.

Here is my schema

  { 
    "_id": ObjectId("52cd49c2f770df1c0b000001")
   "datejoined": ISODate("2014-01-08T12:51:14.0Z"),
   "firstname": "Huston",
   "followers": NumberInt(1),
   "following": [
    {
      "followerid": "52a97985f770dfdc04000000",
      "type": "user",
      "followedon": ISODate("2014-01-23T07:08:43.0Z")
    }
    ],
    "lastname": "Ted",
    "trackscount": NumberInt(0)
   }
   {
   "_id": ObjectId("529726caf770dff815000001")
  "datejoined": ISODate("2014-01-08T12:51:14.0Z"),
  "firstname": "Ted",
  "followers": NumberInt(3),
  "following": [
    {
      "followerid": "528c62406a542f7c6a6bf522",
      "type": "track",
      "followedon": ISODate("2014-01-23T06:53:13.0Z")
    },
    {
      "followerid": "52a97985f770dfdc04000000",
      "type": "user",
      "followedon": ISODate("2014-01-23T07:08:43.0Z")
    },
    {
      "followerid": "52a97985f770dfdc04000023",
      "type": "track",
      "followedon": ISODate("2014-01-24T06:23:30.0Z")
    }
  ],
  "lastname": "Terry",
  "trackscount": NumberInt(0)
}
役に立ちましたか?

解決

Here is the answer to my issue that i found. May be this would be helpful for others. The embedded document ids should be of MongoId object type, in my case i was using followerid as a simple string but it should be a MongoId object. so the schema should be

    { 
    "_id": ObjectId("52cd49c2f770df1c0b000001")
   "datejoined": ISODate("2014-01-08T12:51:14.0Z"),
   "firstname": "Huston",
   "followers": NumberInt(1),
   "following": [
    {
      "followerid": ObjectId("52a97985f770dfdc04000000"),
      "type": "user",
      "followedon": ISODate("2014-01-23T07:08:43.0Z")
    }
    ],
    "lastname": "Ted",
    "trackscount": NumberInt(0)
   }
   {
   "_id": ObjectId("529726caf770dff815000001")
  "datejoined": ISODate("2014-01-08T12:51:14.0Z"),
  "firstname": "Ted",
  "followers": NumberInt(3),
  "following": [
    {
      "followerid": ObjectId("528c62406a542f7c6a6bf522"),
      "type": "track",
      "followedon": ISODate("2014-01-23T06:53:13.0Z")
    },
    {
      "followerid": ObjectId("52a97985f770dfdc04000000"),
      "type": "user",
      "followedon": ISODate("2014-01-23T07:08:43.0Z")
    },
    {
      "followerid": "52a97985f770dfdc04000023",
      "type": "track",
      "followedon": ISODate("2014-01-24T06:23:30.0Z")
    }
  ],
  "lastname": "Terry",
  "trackscount": NumberInt(0)
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top