-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmongo.js
32 lines (26 loc) · 898 Bytes
/
mongo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const mongoose = require('mongoose');
// Connection URI
const uri = "mongodb://localhost:27017/mydatabase"; // Replace with your MongoDB connection string and database
// Connecting to MongoDB
mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => {
console.log("Connected to MongoDB successfully!");
})
.catch((err) => {
console.error("Error connecting to MongoDB:", err);
});
// Example: Defining a simple schema and model
const userSchema = new mongoose.Schema({
name: String,
email: String,
age: Number
});
const User = mongoose.model('User', userSchema);
// Example: Creating a new document
const newUser = new User({ name: "John Doe", email: "[email protected]", age: 30 });
newUser.save()
.then(user => console.log("User saved:", user))
.catch(err => console.error("Error saving user:", err));