“mangosta” Código de respuesta

mangosta

$ npm install mongoose --save
Cute Crab

mangosta

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

mangosta

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

mangosta

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

mangosta

// 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

mangosta

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

mangosta

const adirelad = "a"; 
Upset Unicorn

mangosta

no, he doesn't say among us
Despedite

Respuestas similares a “mangosta”

Preguntas similares a “mangosta”

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código