Using multiple databases in Mongoose (Node.js and MongoDB)

Mongoose, One of the best object modeling framework of Node.js and MongoDB, doesn’t have feature to use multiple databases in single project. Again, to use multiple mongoose instances, Node.js only return loaded module instances from it’s caching system on require(). It means second require() for same module loading will only get the same instance. I am not sure about disabling module caching but I think it is not the good solution. But, as a workaround, we can get the separate mongoose instances by installing the module in separate folder and require() from that folders on each db access.

Let’s assume we have two sub components foo and bar in our project. Foo will use foo_db and bar will use bar_db. So the project structure may be like this.

 

-app_root/
–foo/
—db_access.js
—foo_db_connect.js
—node_modules/
—-mongoose/
–bar/
—db_access.js
—bar_db_connect.js
—node_modules/
—-mongoose/

In foo_db_connect.js

var mongoose = require('./node_modules/mongoose');
mongoose.connect('mongodb://localhost/foo_db');
module.exports = exports = mongoose;

In bar_db_connect.js

var mongoose = require('./node_modules/mongoose');
mongoose.connect('mongodb://localhost/bar_db');
module.exports = exports = mongoose;

In Data access files (db_access.js)

var mongoose = require("./foo_db_connect.js");

Now we can use multiple MongoDB databases with Mongoose.

C Pointers. Confuse ?

When I started learning programming with C++, the learning is stopped when the chapter is reached to pointers. Pointers are often a bit of a stumbling block for people who want to learn C/C++. I read some articles about pointers, but most of them can’t explain the confusing point of pointers for me. In this article, I won’t explain basic and advance usage of pointers. I will explain what I was confused about pointers and what is need to know to clear on pointers. 

What is a Pointer

We can assume pointer is just like other datatypes, but it holds memory addresses rather than values. So, a pointer is a variable that contains a memory address. Please carefully look at the codes below.

int d = 13;
int *e = &d;
printf("d value is %i \n", d); //13
printf("&d value is %p \n", &d); //0xbfffdca8 memory address
printf("e value is %p \n", e); //0xbfffdca8 memory address
printf("*p value is %i \n", *e); //13

Line 1 initiate the value 13 to d.

Line 2, the memory address of d(&d) something will look like 0xbfffdca8 is assigned to *e that is integer pointer type. Still there is no problem. Using pointer is like using normal datatypes. Then, I printed the variables d, e and *e.

Confuse ??

Confusing start here. We assign the address value &d to pointer *e ( can assume like this, int *e = 0xbfffdca8 ), but when we accessing and print *e ,the output is value of d (13) instead of the address. Although we can assume pointers are like normal data types, we also have to assume initialising and accessing ways in pointers are different from normal datatypes.

Although we assigned the memory address to *e, the whole *e is not holding the memory address. Pointer name e is holding the address and ‘*‘ sign is pointing to real value on that address. So *e is printed 13 and e printed 0xbfffdca8.

That is what I was confused in recent years. If you have the same problems on pointers, I hope you are now ok. Then you can now find and practice pointer exercises. Here is one of the good tutorials about pointers. 

http://www.thegeekstuff.com/2011/12/c-pointers-fundamentals/

http://www.thegeekstuff.com/2012/01/advanced-c-pointers/

Literals in Objective-C


 int x = 13;
 double y = 10/3;

In above C code example, primitive values 13 and 10/3 are literals. As Objective-C is a superset of C, valid codes in C are also valid in Objective-C. But in iOS application development environment, object types provide from iOS SDK are being use for literal values instead of primitive data types.

NSString *greeting = @"Hello";

In the above code, @”Hello” is an String type Object literal. Without using @ sign and will be something like this


NSString *greeting = [NSString stringWithUTF8String:"Hello"];

So, the use of that @ sign for object literals are more clear and easy to readable. I think everyone use that literals instead of traditional initialising data type objects.

Here are some examples of object literals. They can also use in Numeric Types, Collection and in Dictionary.

Data Types : Traditional 

NSNumber *boolYES = [NSNumber numberWithBool:YES];
NSNumber *boolNO  = [NSNumber numberWithBool:NO];
NSNumber *charX = [NSNumber numberWithChar:'X'];
NSNumber *fortySevenInt = [NSNumber numberWithInt:47];
NSNumber *fortySevenUnsigned = [NSNumber numberWithUnsignedInt:47U];
NSNumber *fortySevenLong = [NSNumber numberWithLong:47L];
NSNumber *goldenRatioFloat = [NSNumber numberWithFloat:1.61803F];
NSNumber *goldenRatioDouble = [NSNumber numberWithDouble:1.61803];
Data Types : Literals
NSNumber *boolYES = @YES;
NSNumber *boolNO  = @NO;
NSNumber *charX = @'X';
NSNumber *fortySevenInt = @47;
NSNumber *fortySevenUnsigned = @47U;
NSNumber *fortySevenLong = @47L;
NSNumber *goldenRatioFloat = @1.61803F;
NSNumber *goldenRatioDouble = @1.61803;
Collection(Array) : Traditional
NSArray *instruments = [NSArray arrayWithObjects: @"Ocarina", @"Flute", @"Harp", nil];
Collection(Array) : Literals 
NSArray *instruments = @[ @"Ocarina", @"Flute", @"Harp" ];
NSMutableArray *instrumentsMutable = [ @[ @"Ocarina", @"Flute", @"Harp" ] mutableCopy];

Dictionary : Traditional

NSArray *keys   = [NSArray arrayWithObjects:@"Character", @"Weapon", @"Hitpoints", nil];
NSArray *objects = [NSArray arrayWithObjects:@"Zelda", @"Sword", [NSNumber numberWithInt:50], nil];
NSDictionary *stats = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

Dictionary : Literals

NSDictionary *stats = @{ @"Character" : @"Zelda",
                         @"Weapon" : @"Sword",
                         @"Hitpoints" : @50 };

Collection of Popular Open Source Projects on Github


What is Github?

GitHub is a web-based hosting service for software development projects that use the Git revision control system. On 13 September 2012, on their homepage, GitHub announced it had over 2.1 million users hosting over 3.7 million repositories including most popular open-source projects like Android, Linux, JQuery.

Why Github?

Github can provide full requirements for code revision and version control. We can also learn – how do great projects is made, how do they maintain, how do they code with coders around the world. There are many revision control systems over the internet. Among them, Github is simply awesome.

This Collection

It is easy to find the popular projects on Github. But it takes more times than seeing on a bundle collection. I think bundle collections are great help to learn something fast and easy. Now I collect some of them for time saving. Sure, my collection will not complete. I’ll miss more popular projects. Let me know by comments. I’ll edit it.

PHP Projects

JavaScript Projects

Web UI

Python

Ruby

Database

Operating Systems

End User Applications and Developer Tools

SDK Projects