“mangusto” Respostas de código

mangusto

$ npm install mongoose --save
Cute Crab

mangusto

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'));
CodePadding

mangusto

const mongoose = require("mongoose");

// use .env to connect 

const connectDB = async () => {
  try {
    const conn = await mongoose.connect(process.env.MONGO_URI, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    });
    console.log(`mongodb connected :${conn.connection.host}`);
  } catch (error) {
    console.error(`Error:${error.message}`);
    process.exit();
  }
};

module.exports = connectDB;

---------Create Model or Collection-----------
import { Schema, model } from "mongoose"

let userSchema = new Schema({
    name: { type: String, required: true },
    email: { type: String, required: true, unique: true },
    password: { type: String, required: true, },
    role: { type: "String", default: "customer" }
})

export const userModel = model("User", userSchema, "users")
Rasel Hossain

mangusto

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'));
Fair Frog

mangusto

// 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();
      })
  }
});
Stupid Someone

mangusto

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

mangusto

const adirelad = "a"; 
Upset Unicorn

mangusto

no, he doesn't say among us
Despedite

Respostas semelhantes a “mangusto”

Perguntas semelhantes a “mangusto”

Procure respostas de código populares por idioma

Procurar outros idiomas de código