Delete document from mongoDB

It may be a really stupid question, but I'm new to MongoDB, so bear with me. I created a separate ruby ​​class:

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 ... 

Then I do the following with 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">] 

So how can I destroy documents in MongoDB? What am I doing wrong?

+4
source share
4 answers

Thanks for the help on this issue. For everyone who has this problem, I believe this is because I forgot to add the mongodb binaries location to the $PATH variable

In my case, I installed the binaries in /usr/local/mongodb/bin as such, I needed to add export PATH=/usr/local/mongodb/bin:$PATH to my ~/.bash_profile

0
source

Imagine that you want to delete the entire document with an empty "name" field. So here is the code for it:

 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 
+4
source

I don’t know about ruby, but with a command shell this is:

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

Use this code to delete a document by ID:

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

All Articles