Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

mongoose

$ npm install mongoose --save
Comment

mongoose

npm i mongoose // install npm package first
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test');
// create user
const User = mongoose.model('User', { name: String });
const kitty = new User({ name: 'Zildjian' });
kitty.save().then(() => console.log('meow'));
Comment

mongoose node js

// getting-started.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test', {useNewUrlParser: true});
Comment

mongoose

 1. Mongoose is an ODM(object data modeling) library for mongoDB and Node.js,
 higher level of abstraction for mongodb.
 2. **Features:** schemas to model data and relationship,easy data validation,
 simple query Api,middleware etc.
 3.**Schema**(describe structure of data) into 
 **Model**(wapper for schema,providing interface for crud operation)
 
//==== 4)Creating simple tour model===
//server.js
const mongoose= require('mongoose')
const tourSchema= new mongoose.Schema({
     //basic
     name:String,
     rating:Number,
     price:Number,

     //using schema type options
     name:{ type:String,required:[true,'A tour must have a name']},
     rating:{ type:Number,default:4.5},
     price:{type:Number, required:true}
})
const Tour= mongoose.model('Tour',tourSchema);
Comment

mongoose

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test', {useNewUrlParser: true, useUnifiedTopology: true});

const Cat = mongoose.model('Cat', { name: String });

const kitty = new Cat({ name: 'Zildjian' });
kitty.save().then(() => console.log('meow'));
Comment

Mongoose node js

// getting-started.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test', {useNewUrlParser: true});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  // we're connected!
});
Comment

mongoose

const mongoose = require('mongoose');

run().catch(error => console.log(error.stack));

async function run() {
  await mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true });

  // Clear the database every time. This is for the sake of example only,
  // don't do this in prod :)
  await mongoose.connection.dropDatabase();

  const customerSchema = new mongoose.Schema({ name: String, age: Number, email: String });
  const Customer = mongoose.model('Customer', customerSchema);

  await Customer.create({ name: 'A', age: 30, email: 'a@foo.bar' });
  await Customer.create({ name: 'B', age: 28, email: 'b@foo.bar' });

  // Find all customers
  const docs = await Customer.find();
  console.log(docs);
}
Comment

mongoose


module.exports = () => {
  mongoose
    .connect(process.env.MONGODB_URI, {
      dbName: process.env.DB_NAME,
      user: process.env.DB_USER,
      pass: process.env.DB_PASS,
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useFindAndModify: false
    })
    .then(() => {
      console.log('Mongodb connected....');
    })
    .catch(err => console.log(err.message));

  mongoose.connection.on('connected', () => {
    console.log('Mongoose connected to db...');
  });

  mongoose.connection.on('error', err => {
    console.log(err.message);
  });

  mongoose.connection.on('disconnected', () => {
    console.log('Mongoose connection is disconnected...');
  });

  process.on('SIGINT', () => {
    mongoose.connection.close(() => {
      console.log(
        'Mongoose connection is disconnected due to app termination...'
      );
      process.exit(0);
    });
  });
};
Comment

node js + mongoose

const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  // we're connected!
});
Comment

mogoosejs

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test', {userNewurlParser:true});

const Cat = mongoose.model('Cat', { name: String });

const kitty = new Cat({ name: 'Zildjian' });
kitty.save().then(() => console.log('meow'));
Comment

mongoose

module.exports = () => {
  mongoose
    .connect(process.env.MONGODB_URI, {
      dbName: process.env.DB_NAME,
      user: process.env.DB_USER,
      pass: process.env.DB_PASS,
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useFindAndModify: false
    })
    .then(() => {
      console.log('Mongodb connected....');
    })
    .catch(err => console.log(err.message));

  mongoose.connection.on('connected', () => {
    console.log('Mongoose connected to db...');
  });

  mongoose.connection.on('error', err => {
    console.log(err.message);
  });

  mongoose.connection.on('disconnected', () => {
    console.log('Mongoose connection is disconnected...');
  });

  process.on('SIGINT', () => {
    mongoose.connection.close(() => {
      console.log(
        'Mongoose connection is disconnected due to app termination...'
      );
      process.exit(0);
    });
  });
};
Comment

mongoose

// schema creation...

let modal;
SchemaVariable.pre('save', function(next) {
  if (this.isNew) {
    if (!modal) {
      modal = mongoose.model('collectionname');
    }
    modal.find({})
      .then((entries) => {
        this._id = entries.length + 1;
        next();
      })
  }
});
Comment

mongoose

mongoose
    .connect("mongodb://localhost:27017/blogDB", {
        useNewUrlParser: true,
        useUnifiedTopology: true
    })
    .then(() => console.log("Database connected!"))
    .catch(err => console.log(err));
Comment

mongoose node js

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  // we're connected!
});
Comment

node js + mongoose

await mongoose.connect('mongodb://localhost/my_database', {  useNewUrlParser: true,  useUnifiedTopology: true});
Comment

mongoose

const adirelad = "a"; 
Comment

mongoose

no, he doesn't say among us
Comment

PREVIOUS NEXT
Code Example
Javascript :: es6 javascript 
Javascript :: pagination.js example codepen 
Javascript :: toisodatestring 
Javascript :: sort array without mutating js 
Javascript :: javascript call php function with parameters 
Javascript :: The missing value javascript 
Javascript :: vue js encrypt localstorage data 
Javascript :: js remove first character from string 
Javascript :: how to attach function to button sweetalert2 
Javascript :: angularjs format number thousands separator 
Javascript :: dom event 
Javascript :: comment in js 
Javascript :: export html table to excel 
Javascript :: Node.JS mongodb create database 
Javascript :: typeof date 
Javascript :: how to generate random text in vue js 
Javascript :: javascript alert variable 
Javascript :: how to concatenate a string in javascript 
Javascript :: typeof in js 
Javascript :: vue router url string 
Javascript :: jest mock method by name 
Javascript :: invariant failed: you should not use <link outside a <router 
Javascript :: responseText js 
Javascript :: if statement javascript 
Javascript :: javascript Create Strings 
Javascript :: react get route params 
Javascript :: difference between ajax and node js 
Javascript :: emitting event on socket.io using async await 
Javascript :: if or react 
Javascript :: onclick multiple functions react 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =