Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails MongoDB和Mongoid-动态字段_Ruby On Rails_Mongodb_Mongoid - Fatal编程技术网

Ruby on rails MongoDB和Mongoid-动态字段

Ruby on rails MongoDB和Mongoid-动态字段,ruby-on-rails,mongodb,mongoid,Ruby On Rails,Mongodb,Mongoid,我使用的是MongoDB,但我没有真正使用它的动态字段功能 field :fb_followers => Integer field :twitter_followers => Integer field :twitter_rts => Integer field :link_visiting => Integer field :reduce_points_per_day => Integer 我该如何编写它,使这些字段中的每一个对于模型都是可

我使用的是MongoDB,但我没有真正使用它的动态字段功能

  field :fb_followers => Integer
  field :twitter_followers => Integer
  field :twitter_rts => Integer
  field :link_visiting => Integer
  field :reduce_points_per_day => Integer

我该如何编写它,使这些字段中的每一个对于模型都是可选的?

正如@rubish所说,MongoDB中除_id之外的所有字段在MongoDB中都是可选的(除非您使用的是封顶集合,在这种情况下,所有字段都是可选的)

要保存这样的记录而不保存这些字段,可以执行以下操作:

insert({ "some_field_not_related_to_others": 456 })
然后,要使用这些字段保存记录(不需要全部字段),可以选择所有字段:

MongoDB将以insert语句中提供的任何格式保存记录,因为它是无模式的(更多信息可在此处找到:)

这意味着每一行都独立于上面的一行和任何标准化的行或字段,以承载一定数量的行或字段

因此,要“定义它们是可选的”,只需不插入它们即可


如果这不能回答这个问题,也许你可以更具体地说,在哪一方面你是说“可选的”,因为我注意到这是一个ruby问题,所以你可能希望在这里将其设置为可选的,而不是在DB中将其标记为可选的。

因此这里有一些Mongoid特定的信息。首先,确保在您的配置中将
allow_dynamic_fields
设置为
true
(默认值为
true
,但始终可以确保)

下面是世界上最简单的Mongoid类,作为我的示例:

class Foobj
  include Mongoid::Document
  field :static_field
end
因此,我们当然可以正常设置
静态\u字段

ruby-1.9.2-p290 :012 > f = Foobj.create!(:static_field => 'barbaz!')
 => #<Foobj _id: 4ec1f2eb90a110143b000003, _type: nil, regular_field: nil, static_field: "barbaz!">
现在,我已经在Mongoid中“创建”了该字段,我现在可以使用常规机制访问字段,即使它不在我的类定义中:

ruby-1.9.2-p290 :017 > f.dynamic_field
 => "hi!"
另外,请注意,如果加载带有非Mongoid指定字段的Mongoid文档,您确实可以使用相同的方式访问它们:

ruby-1.9.2-p290 :028 > g = Foobj.first
 => #<Foobj _id: 4ec1f2eb90a110143b000003, _type: nil, regular_field: nil, dynamic_field: "hi!", static_field: "barbaz!">
ruby-1.9.2-p290 :029 > g.dynamic_field
 => "hi!"
ruby-1.9.2-p290:028>g=Foobj.first
=> #
ruby-1.9.2-p290:029>g.dynamic_字段
=>“嗨!”
但是,使用动态字段最安全的方法是使用
write\u attribute
read\u attribute
(或它们的快捷方式,
[]=
[]
),因为如果字段不存在,它们不会抛出
NoMethodError


希望有帮助。当你习惯它的时候,它真的很简单。有关更多信息,请参阅:

只是在控制台中玩一些游戏,以说明我所说的“它们是可选的,直到您编写一些验证来检查它们的存在。”

Mongodb文档大部分看起来像是
的输出,如\u文档
。未设置值的键未设置,在mongodb中未设置。我希望这能回答这个问题

class Foo
  include Mongoid::Document

  field :abc, :type => Integer
end
如果要动态添加一个字段:xyz,则可以使用下面的语法

irb(main):005:0> foo = Foo.new
irb(main):006:0> foo.abc = 1
irb(main):009:0> foo["xyz"] = 2  #dynamic field
irb(main):010:0> foo
=> #<Foo _id: 51f23eebd1d872be84000001, abc: 1, xyz: 2>
irb(main):005:0>foo=foo.new
irb(主):006:0>foo.abc=1
irb(主):009:0>foo[“xyz”]=2#动态字段
irb(主要):010:0>foo
=> #

它们是可选的,直到您编写一些验证来检查它们的存在。只有您设置的字段将保存在DB中,其他字段将被悄悄跳过。您能提供一些示例代码吗?添加了一个包含大量Mongoid特定信息的答案。是的,问题更多的是关于等式的Mongoid方面,我知道如何向mongo插入可选记录(这就是我选择mongo的原因)。
ruby-1.9.2-p290 :028 > g = Foobj.first
 => #<Foobj _id: 4ec1f2eb90a110143b000003, _type: nil, regular_field: nil, dynamic_field: "hi!", static_field: "barbaz!">
ruby-1.9.2-p290 :029 > g.dynamic_field
 => "hi!"
class Stat
  include Mongoid::Document

  field :fb_followers, :type => Integer
  field :twitter_followers, :type => Integer
  field :twitter_rts, :type => Integer
  field :link_visiting, :type => Integer
  field :reduce_points_per_day, :type => Integer
end

pry(main)> Stat.create(:fb_followers => 15).as_document
#=> {"fb_followers"=>15, "_id"=>BSON::ObjectId('4ec1ff1a75f38e676400000d')}
pry(main)> Stat.create(:fb_followers => 15, :twitter_rts => 30).as_document
#=> {"fb_followers"=>15, "twitter_rts"=>30, "_id"=>BSON::ObjectId('4ec1ff2b75f38e676400000e')}
pry(main)> Stat.create(:fb_followers => 15, :twitter_followers => 30).as_document
#=> {"fb_followers"=>15, "twitter_followers"=>30, "_id"=>BSON::ObjectId('4ec1ff4775f38e676400000f')}
class Foo
  include Mongoid::Document

  field :abc, :type => Integer
end
irb(main):005:0> foo = Foo.new
irb(main):006:0> foo.abc = 1
irb(main):009:0> foo["xyz"] = 2  #dynamic field
irb(main):010:0> foo
=> #<Foo _id: 51f23eebd1d872be84000001, abc: 1, xyz: 2>