npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

mongodbrelation

v1.1.2

Published

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

Readme

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

so we need a query like get all blogs and join the author in blog document.

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

IN Action for 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

so we need a query like get author where authorid = 100 and append his/her blogs in blogalias

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

IN Action for 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