“Verwenden von Mongoose mit Knoten JS” Code-Antworten

Mongoose in NodeM JS

const kittySchema = new mongoose.Schema({
  name: String
});
Good Gemsbok

Warum wir Mongoose verwenden

Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node. js. It manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB.
Helpful Hummingbird

Knoten JS Mongoose

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

Verwenden von Mongoose mit Knoten JS

// Create an instance of model SomeModel
var awesome_instance = new SomeModel({ name: 'awesome' });

// Save the new model instance, passing a callback
awesome_instance.save(function (err) {
  if (err) return handleError(err);
  // saved!
});
Yellowed Yak

Mongoose in NodeM JS

await fluffy.save();
fluffy.speak();
Good Gemsbok

Mongoose in NodeM JS

// NOTE: methods must be added to the schema before compiling it with mongoose.model()
kittySchema.methods.speak = function speak() {
  const greeting = this.name
    ? "Meow name is " + this.name
    : "I don't have a name";
  console.log(greeting);
};

const Kitten = mongoose.model('Kitten', kittySchema);
Good Gemsbok

Ähnliche Antworten wie “Verwenden von Mongoose mit Knoten JS”

Fragen ähnlich wie “Verwenden von Mongoose mit Knoten JS”

Weitere verwandte Antworten zu “Verwenden von Mongoose mit Knoten JS” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen