Mongoid 不在查询中

编程入门 行业动态 更新时间:2024-10-23 19:28:13
本文介绍了Mongoid 不在查询中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在使用 mongoid 时遇到了一些问题:

I have some trouble with mongoid:

test "Test candidate" do User.create(:id => 1, :sex => User::Male, :country => 1, :city => 1) User.create(:id => 2, :sex => User::Female, :country => 1, :city => 1) User.create(:id => 3, :sex => User::Female, :country => 1, :city => 1) user = User.not_in(:id => [2]).second assert_not_equal(user.id, 2) end

测试失败.我尝试使用 where(:id => {'$nid' => [2]}),但效果相同.

Test failed. I've tried to use where(:id => {'$nid' => [2]}), but it have same effect.

怎么了?如何在 mongoid 中使用不在"状态?

What is wrong? How to use "not in" condition with mongoid?

PS,第二个"没问题,第一个"测试通过,因为 id=1

PS, "second" is ok, with "first" test passed, because id=1

推荐答案

试试这个查询:

user = User.not_in(:_id => [2]).second

在 MongoDB 中,主键的名称为 _id.Mongoid 试图变得友好,并通过在对象模型中将其别名为 id 来部分地向开发人员隐藏这一事实.但是当你进行查询时,它无法判断你想要主键_id还是一些完全普通的字段id.

In MongoDB primary key has name _id. Mongoid tries to be friendly and partially hides this fact from the developer by aliasing it to id in the object model. But when you do queries, it cannot tell if you want primary key _id or some completely ordinary field id.

更多推荐

Mongoid 不在查询中

本文发布于:2023-10-17 13:08:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1501008.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Mongoid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!