One One and Many One model association in nodejs mongodb

Posted By : Parveen Kumar Yadav | 26-Sep-2017

We all know that mongodb is not a relational database, but if we want than we can create all relationship at model levels like one module i have used provide the same i.e Rest-Hapi. Yo can create all the relationship like one-one, one-many, many-one, and many-many by defining them in the model. As you defined it, automatically it insert the data into db according to the relationship you defined. Like if you defined one-one relational-ship then it automatically insert the _id to both models when you hit any API related to them. For one-one association:- Schema field is required for the associations of type ONE_TO_ONE, MANY_ONE. This field  match the association name include  type of ObjectId, and also include a ref property with in the associated model name. Like for example you can define one-one association like:- This is my occupant model


module.exports = function(mongoose) {
    var modelName = "occupant";
    var Types = mongoose.Schema.Types;
    var Schema = new mongoose.Schema({
        phone: {
            type: Types.String
        },
        webAddress: {
            type: Types.String
        },
	user: {
            type: Types.ObjectId,
            ref: "user"
        }
})

 Schema.statics = {
        collectionName: modelName,
        routeOptions: {
            alias: "Occupant",
            associations: {
                user: {
                    type: "ONE_ONE",
                    model: "user"
                }
	// other models association
	   }
	}
}
 

And in User model its look like:-

module.exports = function(mongoose) {
    var modelName = "user";
    var Types = mongoose.Schema.Types;
    var Schema = new mongoose.Schema({
        isActive: {
            type: Types.Boolean,
            default: true
        },
        password: {
            type: Types.String,
            required: true,
            exclude: true,
            allowOnUpdate: false
        },
 occupant: {
            type: Types.ObjectId,
            ref: "occupant"
        },
})
Schema.statics = {
        collectionName: modelName,
        routeOptions: {
            associations: {
               occupant: {
                    type: "ONE_ONE",
                    model: "occupant"
                }
		// other models association
	}
}
}

 

For MANY_TO_ONE OR ONE-TO_MANY :- I have a model user and building . Many users have one building and one building can have many users. So In User model:-


module.exports = function(mongoose) {
    var modelName = "user";
    var Types = mongoose.Schema.Types;
    var Schema = new mongoose.Schema({
        isActive: {
            type: Types.Boolean,
            default: true
        },
        password: {
            type: Types.String,
            required: true,
            exclude: true,
            allowOnUpdate: false
        },
building: {
            type: Types.ObjectId,
            ref: "building"
        },
})

 

and association looks like:-

building: {
                    type: "MANY_ONE",
                    alias: "building",
                    model: "building"
                },

 

In Building model association :-

user: {
                     type: "ONE_MANY",
                     alias: "user",
                     foreignField: "building",
                     model: "user",
                 },
 

So with simple crud operation it also create some other endpoints:- GET /building/{ownerId}/user Get all of the users for a building POST /building/{ownerId}/user Add multiple users to a building DELETE /building/{ownerId}/user Remove multiple users from a building list of users PUT /building/{ownerId}/user/{childId} Add a single user object to a building list of users DELETE /building/{ownerId}/user/{childId} Remove a single user object from a building list of users In this way you can achieve association of one_one and one_many using Rest-Hapi module.

Note:For Many_Many please see my next blog i.e MANY_MANY model association in nodejs mongodb using Rest-Hapi.

 

Hope it would help

Thanks

About Author

Author Image
Parveen Kumar Yadav

Parveen is an experienced Java Developer working on Java, J2EE, Spring, Hibernate, Grails ,Node.js,Meteor,Blaze, Neo4j, MongoDB, Wowza Streaming Server,FFMPEG,Video transcoding,Amazon web services, AngularJs, javascript. He likes to learn new technologies

Request for Proposal

Name is required

Comment is required

Sending message..