MongoDB shell inserts Float when try to insert an Integer/Long
Because of numbers are doubles in Javascript, when yo do this:
> db.test.insert({name: ‘foo’, value: 1});
you get this:
>db.test.findOne({name: ’foo’});
{‘name’: ‘foo’, ‘value’: 1.0}
To avoid this you can use NumberInt or NumberLong:
> db.test.insert({name: ‘foo’, value: NumberLong(1)});
How to delete large amount of data of a MongoDB collection “quickly”
We have a db collection that is around 30 million documents, and I need to trim it down, to only keeping the documents created on the last month.
One approach would be use the remove command with a condition on the created_at field (the collection already have an index on this field):
db.my_collection.remove({created_at: {$lte: new Date("11/01/2012")}});
But this approach will be very slow, instead of that, a better way to do it is rename the current collection (for instance to “old_collection”) using renameCollection. Then performing a query-and-insert from the “old_collection” into “my_collection”:
db.my_collection.renameCollection("old_collection");
db.createCollection("my_collection");
db.my_collection.createIndex(...); // recreate the indexes for the collection
// copy docs from old collection into the new collection
db.old_collection.find(
{created_at: {$gte: new Date("11/01/2012")}
).sort({_id: -1}).forEach(
function(row) { db.my_collection.insert(row); }
);
// drop old collection
db.old_collection.drop();
This approach is typically faster than running a bunch of removes on your data
Drop a database in MongoDB
If you want to delete a database named ‘dummy_db’ just do this:
$ mongo
> use dummy_db
switched to db dummy_db
> db.dropDatabase()
{ "dropped" : "dummy_db", "ok" : 1 }
MongoDB how to compare two IDs fields on same document
To compare two fields on same document in MongoDB you can use a $where (just be aware it will be fairly slow, has to execute Javascript code on every record) :
db.my_collection.find( "this.fieldA > this.fieldB" } );
(check this thread on StackOverflow)
In my case I want to find all the documents where a field id_A is equal to field id_B, so I tried this:
db.my_collection.find( "this.id_A == this.id_B" } );
but this not works because you can’t overload == in javascript, so the way to do is:
db.my_collection.find( "this.id_A.equals(this.id_B)" } );
And from ruby with MongoID you use:
MyCollection.where("$where" => "this.id_A.equals(this.id_B)")
How to install MongoDB in Ubuntu 10.10
10gen publishes apt-gettable packages for MongoDB, those packages are generally fresher than those in Ubuntu.
So, to install MongoDB from 10gen packages do this:
-
Add MongoDB repository into Ubuntu
Add the following line to
/etc/apt/source.listfile:deb http://downloads.mongodb.org/distros/ubuntu 10.10 10gen -
Create PGP key and Install MongoDB
We need to generate key to gain access into MongoDB repository. Use this:
$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
Then update your repository:
$ sudo apt-get update
And install MongoDB:
$ sudo apt-get install mongodb-stable
RSpec with Mongoid
If you are using Mongoid as ORM and RSpec for testing maybe you are interested in use the mongoid-rspec gem.
mongoid-rspec provide several RSpec matchers and macros for Mongoid.
If you are using the new master branch of Mongoid (which is my case), probably you are facing this error when try to run your specs (rake spec):
no such file to load -- mongoid/associations (LoadError)
This is because the new master branch of Mongoid has deleted the file mongoid/associations. You need use the safe_master branch. Or you can try to use this fork of Mongoid-rspec compatible with Mongoid 2.0.0.rc1:
https://github.com/shingara/mongoid-rspec/tree/mongoid-2.0.0.rc1
To do this, just add this gem to your gemfile:
gem 'mongoid-rspec', :git => 'git://github.com/shingara/mongoid-rspec.git',
:branch => 'mongoid-2.0.0.rc1'
Disable start of a service on boot in Ubuntu
For instance, I don’t want that MongoDB automatically starts on boot:
$ sudo update-rc.d -f mongodb remove Removing any system startup links for /etc/init.d/mongodb ... /etc/rc0.d/K20mongodb /etc/rc1.d/K20mongodb /etc/rc2.d/K20mongodb /etc/rc3.d/K20mongodb /etc/rc4.d/K20mongodb /etc/rc5.d/K20mongodb /etc/rc6.d/K20mongodb
Consuming the Twitter data: figthing against the hydra
Aníbal and I were invited to talk in the event “Internet Caracas 2010 - Lets talk about social networks and web business” (Dec 03, 2010)
Our presentation was about our experience doing real time analytics of urls shared in twitter
The way we use NoSQL storages (like MongoDB and Redis) to face this problem. And the several startup iterations, starting with FeedTrace, until InstantPulp, passing through BuzzTrace.
Presentation below:
Consumiendo los datos de Twitter: Luchando contra la Hidra on Prezi