MongoDB 查询问题
我正在使用 Codeigniter 和 Alex Bilbies oAuth2 以及 MongoDB 库开发一个 API。 我需要执行 MongoDB 查询,其中查询 _id 为 1 且访问令牌为 null 的文档。
到目前为止我已经有了这个,但它不起作用:
$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');
如何更改此查询以使其工作?
I am developing an API using Codeigniter and Alex Bilbies oAuth2 and MongoDB libraries.
I need to do a MongoDB query where I do a query for a document with _id of 1 and access token is null.
I have this so far but it does not work:
$exists_query = $this->CI->mongo_db->select('access_token')->where(array('_id' => $session_id, 'access_token' != NULL))->get('oauth_sessions');
I tried this too:
$exists_query = $this->CI->mongo_db->select('access_token')->where(array('_id' => $session_id))->where_not_equal('access_token', NULL)->get('oauth_sessions');
How can I change this query to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来很奇怪..你没有使用mongo提供的id吗?
如果是,则必须使用特殊的 mongoid 函数将原始字符串转换为 mongo id。如果你不使用默认的 mongoid...那么你真的应该使用!
另外为什么你必须设置空值? Mongo 的结构非常不特定,如果不使用某个字段,则不需要为其设置空值.. 只是不包含该字段.. 这不是需要存储空值的 mysql.. 中的每一行mongo db 可以有不同的列..这样,当您查询数据库时,您可以使用 mongo 中的本机方法来检查字段是否存在..
按照应有的方式使用 mongo ..并且您不会遇到这些问题拥有
你的查询应该类似于
This seems weird.. Are you not using the id's that mongo provides??
If you are, you have to use the special mongoid function that converts raw string into a mongo id. If your not using the default mongoid... than you really should!
Also why are you having to set nulls?? Mongo is very structure unspecific, if a field isn't used you dont need to set a null value to it.. just dont include that field.. this isn't mysql where you need to store null values.. each row in the mongo db can have different columns.. that way when you are querying the db you can use the native method in mongo to check is a field exists..
Use mongo the way it should be used.. and you wont have these issues you are having
Your query should look something like like