UNPKG

mongodbrelation

Version:

ONE_TO_MANY, MANY_TO_ONE, ONE_TO_ONE, MANY_TO_MANY realtionships among diffrent collections in mongodb

170 lines (146 loc) 4.8 kB
# README # Follow this for getting started ### What is this module for? ### this module is used for using mongodb as sql database we can perform joins among different collections of our mongodb database ### How do I get set up? ### ``` npm install mongodbrelation ``` ### NEW FEATUES I AM WORKING ON * Nested Joins ### Who do I talk to? ### * kamodh.pandey216@gmail.com ### FEATURES AVAILABLE PERFORM JOINS AMONG DIFFERENT COLLECTIONS OF MONGODB ### USAGE ```json <!-- authors collection --> [ {"_id": 100, "name": "author 1"}, {"_id": 102, "name": "author 2"}, {"_id": 103, "name": "author 3"} ] ``` ```json <!-- blogs collection --> [ {"_id":1, "blogname": "blog1", "description":" blog description 1", "authorid": 100}, {"_id":2, "blogname": "blog2", "description":" blog description 2", "authorid": 100} ] ``` ### ONE_TO_ONE ### let take an example of > ONE_TO_ONE > i.e one blog belongs to one author ### so we need a query like get all blogs and join the author in blog document.<br/> here parent is blog and child is author since author will be appended to blog<br/> **condition: null (since we want all the blogs)**<br/> **joinCollectionName: authors (since we will join author to blogs)**<br/> **joinFieldName: authorid (since parent is blog the key that is present in child is authorid)**<br/> **foreignFieldName: _id (authorid can be mapped to _id with author)**<br/> **aliasname: authoralias (or what ever you prefer for the aliasname you can provide any string)**<br/> **outputfields: null since we want to select all**<br/> **typeofJoin: ONE_TO_ONE**<br/> ### IN Action for ONE_TO_ONE<br/> ```js const mongodbrelation = require('mongodbrelation'); let query = mongodbrelation.joinedQuery(null,"authors", "authorid", "_id", "authoralias", null, "ONE_TO_ONE"); let dbquery = BlogModel.aggregate(query); // where BlogModel is mongoose Model Object dbquery.exec(function(err, result) { console.log(result); }) ``` ### CONSOLE OUTPUT ```json [ { "_id": 1, "blogname": "blog1", "description": " blog description 1", "authorid": 100, "createdAt": "2018-03-12T11:07:05.256+0000", "updatedAt": "2018-03-12T11:07:05.256+0000", "__v": 0, "authoralias": { "_id": 100, "name": "author 1", "createdAt": "2018-03-12T11:07:05.256+0000", "updatedAt": "2018-03-12T11:07:05.256+0000", "__v": 0 } }, { "_id": 2, "blogname": "blog2", "description": " blog description 2", "authorid": 100, "createdAt": "2018-03-12T11:07:05.256+0000", "updatedAt": "2018-03-12T11:07:05.256+0000", "__v": 0, "authoralias": { "_id": 100, "name": "author 1", "createdAt": "2018-03-12T11:07:05.256+0000", "updatedAt": "2018-03-12T11:07:05.256+0000", "__v": 0 } } ] ``` ### ONE_TO_MANY ### let take an example of > ONE_TO_MANY > i.e one author can write may blogs ### so we need a query like get author where authorid = 100 and append his/her blogs in blogalias <br/> here parent is blog and child is author since author will be appended to blog<br/> **condition: {"_id":100} (since we want the author where _id: 100)**<br/> **joinCollectionName: blogs (since we will join blogs to authors)**<br/> **joinFieldName: _id (since parent is author and _id is present in child as collection blogs as authorid)**<br/> **foreignFieldName: authorid (authorid can be mapped to _id with authors collection)**<br/> **aliasname: blogalias (or what ever you prefer for the aliasname you can provide any string)**<br/> **outputfields: null since we want to select all**<br/> **typeofJoin: ONE_TO_MANY**<br/> ### IN Action for ONE_TO_MANY<br/> ```js const mongodbrelation = require('mongodbrelation'); var query = mongodbrelation.joinedQuery({_id: 100},"blogs", "_id", "authorid", "blogalias", null, "ONE_TO_MANY"); let dbquery = AuthorsModel.aggregate(query); // where AuthorsModel is mongoose Model Object dbquery.exec(function(err, result) { console.log(result); }) ``` ### CONSOLE OUTPUT ```json { "_id": 100, "name": "author 1", "createdAt": "2018-03-12T11:07:05.256+0000", "updatedAt": "2018-03-12T11:07:05.256+0000", "__v": 0, "blogalias": [ { "_id": 1, "blogname": "blog1", "description": " blog description 1", "authorid": 100, "createdAt": "2018-03-12T11:07:05.256+0000", "updatedAt": "2018-03-12T11:07:05.256+0000", "__v": 0 }, { "_id": 2, "blogname": "blog2", "description": " blog description 2", "authorid": 100, "createdAt": "2018-03-12T11:07:05.256+0000", "updatedAt": "2018-03-12T11:07:05.256+0000", "__v": 0 } ] } ``` ### Contribution guidelines ### * Writing tests * Code review * Other guidelines