Showing posts with label mongoid. Show all posts
Showing posts with label mongoid. Show all posts

Thursday, 9 April 2015

Mongoid scopes usage

class Person
  include Mongoid::Document
  field :occupation, type: String
  field :age, type: Integer

  scope :rock_n_rolla, -> { where(occupation: "Rockstar") }
  scope :washed_up, where(:age.gt => 30)
  scope :age_or_occupation, ->  {any_of({occupation: "Rockstar"}, {age: 18}) }
  scope :over, ->(limit) { where(:age.gt => limit) }
end

# Find all the rockstars.
Person.rock_n_rolla

# Find all rockstars age greater than 30.
Person.washed_up.rock_n_rolla

# Find all rockstars or other persons have their age as 18 (or operation)
Person.age_or_occupation

# Find a criteria rockstart with 60 as their aget.
Person.rock_n_rolla.over(60)

Tuesday, 24 June 2014

MONGO DB BASICS FOR BEGINNERS


Mapping RDBMS TO MongoDB:

RDBMSMongoDB
DatabaseDatabase
TableCollection
Tuple/RowDocument
columnField
Table JoinEmbedded Documents
Primary KeyPrimary Key (Default key _id provided by mongodb itself)
Database Server and Client
Mysqld/Oraclemongod
mysql/sqlplusmongo

Basic Queries:

Use database:
>use [database_name] #create and switch to that database
Ex: >use mongo
#switch to mongo database

show collections:
>show collections #To show collections in mongo database.

Current Database:
>db #To show current database

To show Databases:
>show dbs #To show all databases
Note: Its not displaying sari database because there is no data in sari database. first insert some records in database.

Drop Database:
>db.dropdatabase()#To drop a current database

Select Records:
>db.collectionName.findOne()
#To show first record in a collection
>db.collectionName.find().pretty
#To show all fields in a format
>db.collectionName.find().limit(5)
>db.collectionName.find( { _id: 5 } )
#displays records with id=5
>db.collection.find( { field: { $gt: value1, $lt: value2 } } );
Ex:>db.students.find( { score: { $gt: 0, $lt: 2 } } )
#To specify ranges
Create a Collection:
>db.createCollection(nameoptions)
#To create a collection
Ex: db.createCollection(student, {name: <string>, qual: <string>, age: <number>})

Insert Values Into Collection:

>db.collectionname.insert( { field1: value1, field2: val2} )
Ex:db.students.insert( { name: "ss", qual: "btech", age: 15 } )

To Remove Specific Documents:
>db.collection.remove()
#to delete a document in collection
Ex:>db.students.remove( { age: { $gt: 20 } } )






Monday, 23 June 2014

How do I create a MongoDB dump/Restore of my database?



To DAMP SPECIFIC DATABASE

Syn: $mongodump --db [database name]
Ex:   $mongodump --db mongo

I t wil create a dumb file in home/dump folder with the name of mongo

The --out option allows you to specify the directory where mongodump will save the backup.mongodump creates a separate backup directory for each of the backed up databases

Syn: $mongodump --db [database name] --out [dumb foldername]
Ex:   $mongodump --db mongo --out saritha

I t wil create a backup files in saritha/dump folder with the name of mongo

To dump specific collection in database:
Syn: $mongodump --db [database name] --collection [collection name]
Ex: $mongodump --db mongo --collection users

To zip the dump file:

Syn: $mongodump -db [dtabasename] --collection [collection name] --out - | gzip > [zip file name].gz

Ex: $mongodump -db mongo --collection users --out - | gzip > mongo1.gz

CHeck it in your home folder with name mongo1.gz

[or] With a date in the filename:

Syn: $mongodump -db [database name] --collection [collection name] --out - | gzip > dump_`date "+%Y-%m-%d"`.gz


TO RESTORE DATABASE


Syn:$mongorestore [database name with path]/

Ex:$mongorestore mongo/

Syn:
$ mongorestore --drop -d <database-name> <directory-of-dumped-backup>

--drop:
The restoration procedure will drop every collection from the target database before restoring the data from the dumped backup.

-d <database-name>:

To specify the database name.