2011-10-12 87 views
1

我正在开发一个API,使用Codeigniter和Alex Bilbies oAuth2和MongoDB库。 我需要做一个MongoDB查询,我在这里查询_id为1且访问令牌为空的文档。MongoDB查询问题

我有这到目前为止,但它不工作:

$exists_query = $this->CI->mongo_db->select('access_token')->where(array('_id' => $session_id, 'access_token' != NULL))->get('oauth_sessions'); 

我想这太:

$exists_query = $this->CI->mongo_db->select('access_token')->where(array('_id' => $session_id))->where_not_equal('access_token', NULL)->get('oauth_sessions'); 

如何更改此查询工作?

回答

0

这似乎很奇怪..你是不是使用了mongo提供的id?

如果你是,你必须使用特殊的mongoid函数将原始字符串转换为mongo id。如果你不使用默认的mongoid ...比你真的应该!

另外,你为什么不得不设置空值? Mongo非常非结构化,如果一个字段没有被使用,你不需要为它设置一个空值..只是不包含该字段..这不是MySQL你需要存储空值..每行在mongo db可以有不同的列..这样,当你查询分贝,你可以使用mongo的本地方法来检查是否存在字段..

使用mongo应该使用它的方式..而你不会有您有

这些问题

您的查询应该是这个样子像

$this->CI->mongo_db->select('access_token')->where(array('_id' => MongoId($session_id), exists => array('access_token' => -1)))->get('oauth_sessions');