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?
FEATURES AVAILABLE
PERFORM JOINS AMONG DIFFERENT COLLECTIONS OF MONGODB
USAGE
<!-- authors collection -->
[
{"_id": 100, "name": "author 1"},
{"_id": 102, "name": "author 2"},
{"_id": 103, "name": "author 3"}
]
<!-- 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
here parent is blog and child is author since author will be appended to blog
condition: null (since we want all the blogs)
joinCollectionName: authors (since we will join author to blogs)
joinFieldName: authorid (since parent is blog the key that is present in child is authorid)
foreignFieldName: _id (authorid can be mapped to _id with author)
aliasname: authoralias (or what ever you prefer for the aliasname you can provide any string)
outputfields: null since we want to select all
typeofJoin: ONE_TO_ONE
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
[
{
"_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
here parent is blog and child is author since author will be appended to blog
condition: {"_id":100} (since we want the author where _id: 100)
joinCollectionName: blogs (since we will join blogs to authors)
joinFieldName: _id (since parent is author and _id is present in child as collection blogs as authorid)
foreignFieldName: authorid (authorid can be mapped to _id with authors collection)
aliasname: blogalias (or what ever you prefer for the aliasname you can provide any string)
outputfields: null since we want to select all
typeofJoin: ONE_TO_MANY
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
{
"_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