蒙古语不在查询中 [英] Mongoid not in query

查看:75
本文介绍了蒙古语不在查询中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对木乃伊有一些麻烦:

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.

怎么了?如何将不在"状态与蒙古性动物一起使用?

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.

这篇关于蒙古语不在查询中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆