DekGenius.com
JAVASCRIPT
mongoose
$ npm install mongoose --save
mongoose
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'));
mongoose node js
// getting-started.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test', {useNewUrlParser: true});
mongoose
1. Mongoose is an ODM(object data modeling) library for mongoDB and Node.js,
higher level of abstraction for mongodb.
2. **Features:** schemas to model data and relationship,easy data validation,
simple query Api,middleware etc.
3.**Schema**(describe structure of data) into
**Model**(wapper for schema,providing interface for crud operation)
//==== 4)Creating simple tour model===
//server.js
const mongoose= require('mongoose')
const tourSchema= new mongoose.Schema({
//basic
name:String,
rating:Number,
price:Number,
//using schema type options
name:{ type:String,required:[true,'A tour must have a name']},
rating:{ type:Number,default:4.5},
price:{type:Number, required:true}
})
const Tour= mongoose.model('Tour',tourSchema);
mongoose
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'));
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
const mongoose = require('mongoose');
run().catch(error => console.log(error.stack));
async function run() {
await mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true });
// Clear the database every time. This is for the sake of example only,
// don't do this in prod :)
await mongoose.connection.dropDatabase();
const customerSchema = new mongoose.Schema({ name: String, age: Number, email: String });
const Customer = mongoose.model('Customer', customerSchema);
await Customer.create({ name: 'A', age: 30, email: 'a@foo.bar' });
await Customer.create({ name: 'B', age: 28, email: 'b@foo.bar' });
// Find all customers
const docs = await Customer.find();
console.log(docs);
}
mongoose
module.exports = () => {
mongoose
.connect(process.env.MONGODB_URI, {
dbName: process.env.DB_NAME,
user: process.env.DB_USER,
pass: process.env.DB_PASS,
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
})
.then(() => {
console.log('Mongodb connected....');
})
.catch(err => console.log(err.message));
mongoose.connection.on('connected', () => {
console.log('Mongoose connected to db...');
});
mongoose.connection.on('error', err => {
console.log(err.message);
});
mongoose.connection.on('disconnected', () => {
console.log('Mongoose connection is disconnected...');
});
process.on('SIGINT', () => {
mongoose.connection.close(() => {
console.log(
'Mongoose connection is disconnected due to app termination...'
);
process.exit(0);
});
});
};
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
module.exports = () => {
mongoose
.connect(process.env.MONGODB_URI, {
dbName: process.env.DB_NAME,
user: process.env.DB_USER,
pass: process.env.DB_PASS,
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
})
.then(() => {
console.log('Mongodb connected....');
})
.catch(err => console.log(err.message));
mongoose.connection.on('connected', () => {
console.log('Mongoose connected to db...');
});
mongoose.connection.on('error', err => {
console.log(err.message);
});
mongoose.connection.on('disconnected', () => {
console.log('Mongoose connection is disconnected...');
});
process.on('SIGINT', () => {
mongoose.connection.close(() => {
console.log(
'Mongoose connection is disconnected due to app termination...'
);
process.exit(0);
});
});
};
mongoose
// 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();
})
}
});
mongoose
mongoose
.connect("mongodb://localhost:27017/blogDB", {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log("Database connected!"))
.catch(err => console.log(err));
mongoose node js
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
// we're connected!
});
node js + mongoose
await mongoose.connect('mongodb://localhost/my_database', { useNewUrlParser: true, useUnifiedTopology: true});
mongoose
mongoose
no, he doesn't say among us
© 2022 Copyright:
DekGenius.com