Category Archives: Database

Mongo Shell Console

Start mongo console:

 

Show databases:

 

Select a database:

 

Show selected database name:

 

Drop database:

 

Show collections in selected database

 

Select first 10 records from person_account collection:

 

Drop collection:

 

Insert a file in database mydata using mongofiles:

 

Files will be added to collections: fs.chunks and fs.files
List files:

 

Dump databse bikeshop in directory mongo_dump:

 

List records where uploadDate is greater than 26/10/2012:

 

 

Spring Data Mongo DB

Spring MVC Project with maven, mongodb, spring-data

This will contain only snippets of code. There are small dependencies that are not listed in these blocks of code.

I am running mongoDB in a remote Centos 6.2 Server.

Main Database is a PostgreSQL. Upper layers in MVC Service classes will try to fetch data from mongoDB. If is not available, will use SQL DB and update mongoDB. My application uses mongo for caching purposes and writes denormalized relationships in mongoDB.

pom.xml

 

web.xml

 

applicationContext.xml

 

A very simple entity:

A ultra simple Dao Repository:

 

Service class for Repository (contains JPA code for data transfer from JPA Repository to Mongo Repository, can be ignored):

Unit test for this implementation:

 

MySQL

Import dump:

Create dump:

Drop:

Create:

Update:

Insert:

Select duplicates:

Add new column and update Primary Key

INNER Join:

PostgreSQL

Create database:

 

Delete database:

 

Create table:

 

Delete table:

 

Insert

Dump

 

Restore

 

SELECT COUNT(*)

 

Create user

 

Grant privileges

 

Rename column

 

Change password

 

Add auto-increment pk to existing table, where ID is pk

 

Modify sequence number

 

Delete sequence

 

Change column constraint to not null

 

Change column type from integer to smallint

 

Change column type from varchar (20) to varchar (40)

 

Change column type from date to timestamp without time zone

 

Delete column

 

Add column

 

Rename column

 

Add foreign key

 

Delete constraint

 

Delete data

 

Select A where B has 0 entries