“mangusta” Kod odpowiedzi

mangusta

$ npm install mongoose --save
Cute Crab

mangusta

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

mangusta

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

mangusta

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

mangusta

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

mangusta

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

mangusta

const adirelad = "a"; 
Upset Unicorn

mangusta

no, he doesn't say among us
Despedite

Odpowiedzi podobne do “mangusta”

Pytania podobne do “mangusta”

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu