Mongoid - 数组管理?插入唯一值,如果存在则删除值?

发布于 2024-10-12 05:29:53 字数 695 浏览 3 评论 0原文

我正在尝试做一些相当简单的事情,我相信:

1)仅当该值尚不存在时才在数组字段中插入该值

2)如果数组中存在该值则删除该值

我只是不知道如何执行此操作事情...目前我只是插入我的值而不检查它是否已经存在: myArray << obj.id

谢谢,

Alex

ps:使用Rails 3.0.3,mongo 1.1.5和mongoid 2.0.0.rc5

ps2:这是mongodb语法来实现我想要的,但我不知道如何在mongoid中做到这

{ $addToSet : { field : value } }

一点仅当 field 是现有数组时,才将 value 设置为数组,否则如果 field 不存在,则将 field 设置为数组值。如果字段存在但不是数组,则会引发错误情况。

要添加多个值st.update

{ $addToSet : { a : { $each : [ 3 , 5 , 6 ] } } }
$pop

{ $pop : { field : 1  } }

删除数组中的最后一个元素(在 1.1 中添加)

{ $pop : { field : -1  } }

删除数组中的第一个元素(在 1.1 中添加) |

I am trying to do something rather simple I believe:

1) insert a value in an array field only if that value is not already present

2) remove a value if it exists in the array

I have just no idea how to do any of this things... for the moment I am just inserting my value without checking if it exists already: myArray << obj.id

Thanks,

Alex

ps: using Rails 3.0.3, mongo 1.1.5 and mongoid 2.0.0.rc5

ps2: this is the mongodb syntax to achieve what I want, but I have no idea how to do this in mongoid

{ $addToSet : { field : value } }

Adds value to the array only if its not in the array already, if field is an existing array, otherwise sets field to the array value if field is not present. If field is present but is not an array, an error condition is raised.

To add many valuest.update

{ $addToSet : { a : { $each : [ 3 , 5 , 6 ] } } }
$pop

{ $pop : { field : 1  } }

removes the last element in an array (ADDED in 1.1)

{ $pop : { field : -1  } }

removes the first element in an array (ADDED in 1.1) |

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月亮是我掰弯的 2024-10-19 05:29:53

您想要使用 add_to_set 方法,如下所示(在某种程度上):http: //mongoid.org/en/mongoid/docs/persistence.html#atomic

示例:

model = Model.new
model.add_to_set(:field, value)
model.save

您可以给它一个值,甚至一个值数组。当将数组的每个元素添加到您指定的字段时,后者将使用 mongo 的 $each 限定符以及 $addToSet

You want to use the add_to_set method, as documented (somewhat) here: http://mongoid.org/en/mongoid/docs/persistence.html#atomic

Example:

model = Model.new
model.add_to_set(:field, value)
model.save

You can give it a single value or even an array of values. The latter will use mongo's $each qualifier along with $addToSet when adding each element of your array to the field you specify.

梦情居士 2024-10-19 05:29:53

根据 Mongoid googlegroup 的 Chris Hawk 的说法:

Mongoid 文档中的数组是简单的 Ruby 数组。请参阅文档
Array 类: http://www.ruby-doc.org/core/classes /Array.html

因此,对于插入,您可以简单地执行以下操作:

array << object unless array.include?(object) 

对于删除:

array.delete(object) 

As per Chris Hawk from Mongoid googlegroup:

Arrays in Mongoid documents are simple Ruby arrays. See the docs for
the Array class: http://www.ruby-doc.org/core/classes/Array.html

So, for insertion you can simply do:

array << object unless array.include?(object) 

And for removal:

array.delete(object) 
书信已泛黄 2024-10-19 05:29:53

值得一提的是,在 mongoid 中,从 2.0.0pre4 开始,我没有看到任何 addToSet 支持。 mongo_mapper (虽然 imo 维护较少:( )通过 Push_uniq 方法支持此功能。

除此之外,在 mongoid 中,如果您直接使用关系方法,则不需要执行包含?如果您正在使用数组直接,你这样做

class Person
   include Mongoid::Document
   has_and_belongs_to_many :pets ## or has_many :pets, :stored_as => :array if your not using the latest mongoid 
end

#when using .pets no need to check, mongoid checks for you to ensure you can only add the same pet once.
pet = Pet.find("294x29s9a9292")
p = Person.find("42192189a92191a")
p.pets << pet

#when using pet_ids, you do need to check:
pet = Pet.find("294x29s9a9292")
p = Person.find("42192189a92191a")
p.pet_ids << pet.id unless p.pet_ids.include? pet.id

worth mentioning, in mongoid, as of 2.0.0pre4 I do not see any addToSet support. mongo_mapper (while imo less maintained :( ) supports this via push_uniq method.

short of that, in mongoid, if you are working with the relationship method directly, you don't need to do the include?. if you are working with the array directly, you do.

Example:

class Person
   include Mongoid::Document
   has_and_belongs_to_many :pets ## or has_many :pets, :stored_as => :array if your not using the latest mongoid 
end

#when using .pets no need to check, mongoid checks for you to ensure you can only add the same pet once.
pet = Pet.find("294x29s9a9292")
p = Person.find("42192189a92191a")
p.pets << pet

#when using pet_ids, you do need to check:
pet = Pet.find("294x29s9a9292")
p = Person.find("42192189a92191a")
p.pet_ids << pet.id unless p.pet_ids.include? pet.id
庆幸我还是我 2024-10-19 05:29:53

您可以使用 拉取运算符,即原子,删除单个元素。下面是一个示例,其中 l 是对具有数组字段 array 的文档的引用:

l.array = [1, 2, 3]
l.save
l.pull(array: 1) # atomically removes 1, leaving [2, 3]

可以使用 add_to_set,如前所述:

l.array = [2, 3]
l.save
l.add_to_set(array: 1) # atomically adds 1, resulting in [2, 3, 1]

这是有关 Atomic 运算符的当前 Mongoid 文档的链接。

You can use the pull operator, which is atomic, to remove a single element. Here's an example, where l is a reference to a document with an array field array:

l.array = [1, 2, 3]
l.save
l.pull(array: 1) # atomically removes 1, leaving [2, 3]

Adding can be done with add_to_set, as previously mentioned:

l.array = [2, 3]
l.save
l.add_to_set(array: 1) # atomically adds 1, resulting in [2, 3, 1]

Here's a link to the current Mongoid documentation on Atomic operators.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文