DekGenius.com
JAVASCRIPT
mongoose in node.js
// getting-started.js
const mongoose = require('mongoose');
main().catch(err => console.log(err));
async function main() {
await mongoose.connect('mongodb://localhost:27017/test');
}
mongoose node js
// getting-started.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test', {useNewUrlParser: true});
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!
});
mongoose in nodem js
const kittySchema = new mongoose.Schema({
name: String
});
node js + mongoose
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
// we're connected!
});
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'));
mongoose node js
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
// we're connected!
});
using mongoose with node 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!
});
node js + mongoose
await mongoose.connect('mongodb://localhost/my_database', { useNewUrlParser: true, useUnifiedTopology: true});
mongoose in nodem js
const Kitten = mongoose.model('Kitten', kittySchema);
mongoose in nodem js
const silence = new Kitten({ name: 'Silence' });
console.log(silence.name); // 'Silence'
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);
mongoose in nodem js
const fluffy = new Kitten({ name: 'fluffy' });
fluffy.speak(); // "Meow name is fluffy"
mongoose in nodem js
await fluffy.save();
fluffy.speak();
mongoose in nodem js
const kittens = await Kitten.find();
console.log(kittens);
mongoose in nodem js
await Kitten.find({ name: /^fluff/ });
© 2022 Copyright:
DekGenius.com