删除Mongoid中的嵌入式文档 [英] remove an embedded document in mongoid

查看:73
本文介绍了删除Mongoid中的嵌入式文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目模型,其中仅包含一个名称字段,并且其中还包含与line_items的嵌入式关系. 课堂项目 包含mongoid :: document 栏位:名称 embeds_many:line_items 结束

I have a projects model with just a name field and in it also the embedded relation to line_items. class Project include mongoid::document field :name embeds_many :line_items end

  class LineItem
   include mongoid::document
   field :title
   embedded_in :project, :inverse_of => :line_items
  end

我想这更多是mongo驱动程序的问题:如果我有这样的文档

I suppose this is more of the mongo driver question: if I had such a document

db.project.find()[0]
      {
        _id : 123, 
        name : "housework", 
        line_items:[
         { title : "clean fridge", _id : 601},
         { title : "clean tub",    _id : 602},
         { title : "clean oven",   _id : 603}
        ]
      }

  • 1)我该如何在mongo控制台中更新ID为601的订单项?
  • 2)如何删除它?
  • 谢谢!

    推荐答案

    当前的Mongoid(2.0.0)允许:

    Current Mongoid (2.0.0) allows:

    @category = @list.categories.find(params[:id])
    @category.delete
    

    结果数据库查询/更新如下:

    And the resulting database query/update looks like:

    MONGODB test ['lists'].update({"_ id" => BSON :: ObjectId('4d9522315569b11918000019')}, {"$ pull" => {"categories" => {"_ id" => BSON :: ObjectId('4d9523e05569b1191800001f')}}))

    MONGODB test['lists'].update({"_id"=>BSON::ObjectId('4d9522315569b11918000019')}, {"$pull"=>{"categories"=>{"_id"=>BSON::ObjectId('4d9523e05569b1191800001f')}}})

    另请参见 http://mongoid.org/docs/persistence/上的最后一个示例

    Also see the last example on http://mongoid.org/docs/persistence/

    请注意,我对此进行了尝试,可以与ActiveRecord(@ list.categories.delete(xx))一起使用,但似乎没有任何效果.

    Note, I tried variations on this that would have worked with ActiveRecord (@list.categories.delete(xx)) and those do not seem to have any effect.

    这篇关于删除Mongoid中的嵌入式文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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