PHP+MongoDB-使用$elematch从集合内的数组中获取条目不起作用


PHP + MongoDB - Fetch entry from array inside collection with $elemMatch not working

我有以下对象:

{
    users: {
        _id: "users",
        entries: 
        [
            {
                _id: 1,
                username: "taxicala",
                password: "password"
            },
            {
                _id: 2,
                username: "guest",
                password: "guest"
            }
        ]
    }
}

我只想选择"entries"数组中的一个用户。我正在尝试以下操作:

$entry = $this->_users->find(
             array('entries' => 
                 array('$elemMatch' => 
                     array('username' => 'taxicala')
                 )
             )
         );

我得到的结果是一个空对象:

{ }

我在谷歌或这里找不到任何关于这件事的线索。我做错什么了吗?我是不是错过了什么?或者PHP不支持"$elemMatch"?

谢谢!

$elemMatch是受支持的,但就文档而言,entries位于用户object内部。因此,至少在您的查询中,您必须输入array('users.entries' => ...)。我也不明白你为什么需要使用$elemMatch。

db.coll.find({"users.entries.username" : "taxacala"})输出用户名为"taxacala"的文档。