Giter VIP home page Giter VIP logo

mongodb-moongoose's Introduction

๐Ÿ‘ Why shoud we use mongoose

Technical Stack

  • Mongoose
  • MongoDB
  • Javascript
  • Node.js

Overview

As below, I could see mongoose is much more simpler and highly readable comparing to using mongoDB itself.

Comparison Code

MongoDB

 import { CURSOR_FLAGS, MongoClient } from "mongodb";

 // Replace the uri string with your MongoDB deployment's connection string.
 const uri = "mongodb://localhost:27017";
 const client = new MongoClient(uri);
 async function run() {
   try {
     const database = client.db("fruitsDB");
     const fruit = database.collection("fruit");
  
     const doc = [{ name: "Apple", score: 8, review: "Greate fruit" },
     { name: "Orange", score: 6, review: "Kinda sour" },
     { name: "Banana", score: 9, review: "Great stuff!" }];
     // create a document to inser
     const options = {ordered: true}
     const result = await fruit.insertMany(doc,options);
     console.log(`A document was inserted with the _id: ${result.insertedCount}`)
     const cursor = await fruit.find()
     await cursor.forEach((doc)=>console.log(doc))
   } finally {
     await client.close();
   }
}

run().catch(console.dir);

I could check MongoDB library code keeps going inside and inside of bracket as I write code.

Mongoose

import mongoose from "mongoose";

mongoose.connect('mongodb://localhost:27017/fruits',{useNewUrlParser:true});

const fruitSchema = new mongoose.Schema({
    name : {
        type: String,
        required : [true,"Please check your data entry, no name specified!"]
    },
    rating : {
        type: Number,
        min : 1,
        max : 10
    },
    review : String
});

const Fruit = mongoose.model("Fruit", fruitSchema);

const personSchema = new mongoose.Schema({
    name: String,
    age: Number,
    favoriteFruit : fruitSchema
});

const Person = mongoose.model("Person",personSchema);


person.save().then((value)=>{console.log(value);});


const strawberry = new Fruit({
    name : "Strawberry",
    score : 7,
    review : "This was not bad!"
})

strawberry.save();

await Person.updateOne({name : "Junho Shin"}, {favoriteFruit : strawberry});

This code looks way simpler and highly readable and easy.

mongodb-moongoose's People

Contributors

shinjuno123 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.