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
Markdown
Follow this for getting started
this module is used for using mongodb as sql database
we can perform joins among different collections of our mongodb database
```
npm install mongodbrelation
```
* Nested Joins
* kamodh.pandey216@gmail.com
PERFORM JOINS AMONG DIFFERENT COLLECTIONS OF MONGODB
```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}
]
```
let take an example of
> ONE_TO_ONE
> i.e one blog belongs to one author
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/>
```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);
})
```
```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
}
}
]
```
let take an example of
> ONE_TO_MANY
> i.e one author can write may blogs
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/>
```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);
})
```
```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
}
]
}
```
* Writing tests
* Code review
* Other guidelines