To establish a connection between Node.js and MongoDB, follow these steps:
install mongodb --save
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017/myproject';
MongoClient.connect(url, function(err, client) {
console.log("Connected successfully to server");
const db = client.db('myproject');
client.close();
});
db.collection('users').insertOne({
name: "John Doe",
age: 25
}, function(err, result) {
console.log("Inserted document with _id: " + result.insertedId);
});
client.close(function(err) {
console.log("Closed MongoDB connection");
});
With these steps, you can establish a connection between Node.js and MongoDB and start performing database operations.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-04-08 11:00:00 +0000
Seen: 8 times
Last updated: Jul 28 '21
What is the process of integrating API data into MongoDB using Spark/Python?
How can additional fields that have been transformed be queried in MongoDB?
What is the difference between indexing in Elasticsearch and MongoDB?
When data is updated in MongoDB, why does Logstash not reflect the changes?
How can the Mongo Operator be used to deploy MongoDB in EKS/EFS?
How can data be extracted from Azure DocumentDB for exporting purposes?
How can I link or integrate mongodb with Google Data Studio?