2011-06-11 157 views
4

这可能是一个非常愚蠢的问题,但我是MongoDB的新手,请耐心等待。我创建了一个独立的Ruby类:从mongoDB中删除文档

require 'rubygems' 
require 'mongo' 

require 'bson' 
require 'mongo_mapper' 

MongoMapper.database = "testing" 

class Twit 
    include MongoMapper::Document 

    key :id,   Integer, :unique => true 
    key :screen_name, String, :unique => true 

... 

那我该用IRB

>> twit = Twit.all.first 
=> #<Twit _id: BSON::ObjectId('4df2d4a0c251b2754c000001'), id: 21070755, screen_name: "bguestSB"> 
>> twit.destroy 
=> true 
>> Twit.all 
=> [#<Twit _id: BSON::ObjectId('4df2d4a0c251b2754c000001'), id: 21070755, screen_name: "bguestSB">] 

以下那么,如何销毁文件在MongoDB中?我究竟做错了什么?

+0

抱歉,我不能有更多的帮助。 – 2011-06-11 05:08:53

+0

如果你'Twit.find('4df2d4a0c251b2754c000001')。destroy'然后'Twit.all'会发生什么? – 2011-06-11 05:13:41

+0

@mu,条目似乎仍然存在。我能够摆脱保存在数据库中的文件的唯一方法是删除整个数据库。有没有什么方式可以阻止我的系统被删除? – Intentss 2011-06-13 18:02:13

回答

0

感谢在这个问题上的一切帮助。对于其他人谁都有这个问题,我相信这是因为我忘了在我的案例中/usr/local/mongodb/bin安装二进制文件MongoDB的二进制文件的位置添加到$PATH变量

因此我需要添加export PATH=/usr/local/mongodb/bin:$PATH到我的~/.bash_profile

0

不知道关于Ruby,但与命令行shell,它是:

db.Twit.remove({_id: '4df2d4a0c251b2754c000001'}); 
4

想象一下,您要删除所有具有空的“名称”字段的文档。因此,这里是它的代码:

require 'rubygems' 
require 'mongo' 

db = Mongo::Connection.new("localhost").db("db_name") 
coll = db.collection("coll_name") 

coll.find({:name => ""}).each do |empty_doc| 
    coll.remove(empty_doc) 
end 
0

使用此代码通过ID删除文件:

collection.remove({"_id" => BSON::ObjectId("4df2d4a0c251b2754c000001")})