我试图通过匹配它的子文档的id来从集合中检索数据,但是它没有给我任何结果。这是我的密码
$usrcollection = $db->users;
$where = array('following'=> array('followerid' => '52a97985f770dfdc04000000', 'type' => "'user'"));
$usrcursor = $usrcollection->find($where);我也尝试过使用$elemMatch,但是结果是一样的。
$where = array('following' => array(
'$elemMatch' => array(
'followerid' => '52a97985f770dfdc04000000',
'type' => "'user'")
)
);谁来帮我解决这个问题。
这是我的模式
{
"_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)
}发布于 2014-01-29 07:18:44
这是我发现的问题的答案。也许这会对其他人有帮助。嵌入的文档it应该是MongoId对象类型,在我的示例中,我使用的是一个简单的字符串,但它应该是一个MongoId对象。所以模式应该是
{
"_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)
}https://stackoverflow.com/questions/21332024
复制相似问题