Code Monkey home page Code Monkey logo

mongo-reference's Introduction

Mongo-reference

db -> collection -> document

db.users.insetOne({name: "Bipul"})

db.users.find().sort({name: -1}).limit(2)

db.users.find().sort({age: -1, name: -1}).limit(3) //sort by age in rev order(99->0) if same age then sort by name in rev order

like where in sql db.users.find({age: 21}) //unlike js find it returns all users whose age is 26

like select in sql db.users.find({name: "Bipul"}, {name: 1, age: 1, _id: 0})

db.users.find({name: "Bipul"}, {age: 0})

mongoose -> Schema, query, model we can add methods, validations, queries , virtuals and middlewares to the schema

userSchema.methods.sayHi = function(){ clg (HI ${this.name}) } //used on instances

userSchema.methods.findByName = function(){ return this.find({name: new RegExp(name, 'i')}) } //used on models

userSchema.methods.byName = function(){ return this.where({name: new RegExp(name, 'i')}) } // chained on methods in models

userSchema.virtual("namedEmail").get(function(){ return ${this.name} } )// only in code not saved in db

middleware

userschema.pre('save', function(next){ this.updatedAt = Date.now() next() })

//cant use this, as it passes the doc that is saved userSchema.post('save', function(doc, next){ doc.sayHi() next() })

https://github.com/akashshyamdev/mongoose-query-methods-cheatsheet

https://drive.google.com/file/d/1ZEcnJi6oglVQ5qkVnumYdPXvyVHBk7Ck/view?usp=sharing

https://mongoosejs.com/docs/queries.html

mongo-reference's People

Contributors

itsmebipul00 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.