Posts

Showing posts with the label MongoDB

MongoDB Helpful Queries

Image
While doing the development everyones know that no one can remember all of the queries and commands. So in the blog I am covering most used Mongo DB queries. Mongo DB Queries By Education Funda Mongo DB Queries For Studio 3T and In Your Code Fetch Queries: db . getCollection ( "collectionName" ). find ( { FILTER_COLUMN_NAME : 'VALUE' }, { PROJECTION_COLUMN_NAME_1 : 1 , PROJECTION_COLUMN_NAME_2 : 1 } ) Get Duplicates Records db . getCollection ( "collectionName" ). aggregate ([ { "$group" : { "_id" : "$DUP_COL_NAME" , "count" : { "$sum" : 1 } } }, { "$match" : { "_id" : { "$ne" : null } , "count" : { "$gt" : 1 } } }, { "$project" : { "DUP_COL_NAME" : "$_id" , "_id" : 0 } } ]) Count Query db . getCollection ( "collectionName" ). countDocuments ( { FILTER_COL_1 : ...

MongoDB Mongoose Use Lookup Guide

Image
In This Blog We Will See Complete Guide For Using Joins With Lookup in Mongoose MongoDB Mongoose - Education Funda Simply in MongoDB Mongoose we can use Aggregate Query with $ lookup to get data basis upon JOINS. such as below: await MainModel . aggregate ([ { $match : { "COLUMN_NAME" : value } }, { $lookup : { from : 'relation_collection_name' , localField : 'relation_collection_primary_key' , foreignField : 'this_table_foreign_key' , as : 'ALIASING' } } ]); With above query new column added with ALIASING in which relation collection data will also come. To get data in Object format instead of Array we can use $unwind like below: await MainModel . aggregate ([ { $match : { "COLUMN_NAME" : value } }, { $lookup : { from : 'relation_collection_name' , ...

How To Check MongoDB ID Valid Or Not

Image
As you must aware about the object ID in mongo database or MongoDB is 24 characters long so for check is it valid id or not there is no direct validation method in validations package such as node-input-validator. MongoDB - Education Funda So for checking entered id is valid or not we can us below tech-tics: Approach 1 - Simple JS Validation var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$") checkForHexRegExp.test("i am a string") // false checkForHexRegExp.test("5e63c3a5e4232e4cd0074ac2") // true Approach 2 - With Mongo DB Method (Best Way) var ObjectID = require("mongodb").ObjectID ObjectID.isValid("i am a string") // false ObjectID.isValid("5e63c3a5e4232e4cd0274332") // true With Node Input Validator You Can Use Below Method: const id_exists = "required|string|minLength:24|maxLength:24|idExists"   const validator = require ( "node-input-validator" ) const mongoose = require ( 'mongoose...

Standard Way To Create Node JS Fresh Project

Image
As you know in programming world JavaScript is becoming quite famous programming language so in this blog we will discuss how we can create fresh project of NodeJS in standard way. NodeJS - Education Funda Requirements Any server should be installed either Apache or Nginx. NodeJS and NPM should be installed in the operating system.  Steps to Install Fresh Node JS Project: First open your terminal and go to your project directory then run following commands ~ npm init (after hitting this command it will ask some basic questions you can answer it as per your need.   ~ npm init ~ npm install express (it will install express server for your project) ~ npm install mongodb (optional - if you want to use mongo) ~ npm install mongoose (optional - if you want to use mongo) ~ npm install nodemon --save-dev (nodemon package is using for automatically refresh after any change and we're using save dev here because we want to install it for development purpose only) To do setup of licens...

Database Seeding in NodeJS with MongoDB

Image
Seeders in NodeJS Database seeding is a process in which we can put predefined data or dummy data in database as per need. such as the data of countries, cities, etc. is fixed data for everyone for these kind of data we can create database seeding files and we can put all the data into database with the help of 1 command only. Step 1 : We should have model firstly for which we want to create seeding so here is an example model Product. productModel.js Step 2 : As we want to run 'database seeders' separately in one command so have can create one file for seeders and in this file we also need to add database connection so that we can communicate with database because here app.js/index.js database connection doesn't work as we want to run this separately so example seeder.js file is: Seeders.js   Step 3 : Now we can run command node seeders.js to create seeding in database Thanks for reading this article I hope it will help you for creating seeders in node js with mongo datab...

Solve MongoDB.Service: Failed With Result 'Exit-Code'

Image
Solve MongoDB Failed Service Error This worked for me and I didn't reinstall MongoDB. I have created manually sudo mkdir /var/lib/mongodb sudo mkdir /var/log/mongodb   and changed owner permission for both sudo chown -R mongodb:mongodb /var/lib/mongodb sudo chown -R mongodb:mongodb /var/log/mongodb   pgrep mongod sudo service mongodb start sudo service mongodb status Please share your feedback in comment section. Thanks

Top MongoDB Interview Questions

Image
MongoDB is a document database with the scalability and flexibility that you want with the querying and indexing that you need. Education Funda Let's Jump Into Directly Top 10 MongoDB Interview Questions: Important Notes BSON vs JSON JSON is Standard File Format whereas BSON is Binary File Format. BSON is faster then JSON because it is more efficient and MongoDB stores data in BSON format via MongoDB drivers data converted when we save or update any data. JSON takes less storage space whereas BSON takes more space as compared between both. We can embed 100 JSON formats inside JSON maximum. BSON is an encoded format of data so not easily readable for humans whereas JSON we can easily read. Objectld is composed of: Timestamp Client machine ID Client process ID 3 byte increment counter UpdateOne() vs ReplaceOne() With replaceOne () you can only replace the entire document, while updateOne () allows for updating fields.   Since replaceOne () replaces the entire document - fields in th...