Connect to MongoDB in Quasar / VueJS - mongodb

I am trying to setup mongoDB in my Project (using Quasar Framework)
What I did for preparation:
Setup MongoDB Atlas on Windows and create a user, a database and a collection
Create a Connection String
installed mongodb in node_modules (npm install —-save mongodb)
What I tested
Using axios to get test data in JSON format (worked)
Using axios to access the mongoDB (Didn't find a way to do it - Maybe this is not supported)
creating a separate js file to connect to mongoDB (see below)
Access mongoDB via js
After I failed to access mongoDB with axios I tried to connect using this code:
const mongo = require('mongodb')
const MongoClient = mongo.MongoClient
const uri = YOUR_CONNECTION_STRING
var client;var mongoClient = new MongoClient(uri, { reconnectTries :
Number.MAX_VALUE, autoReconnect : true, useNewUrlParser : true })
mongoClient.connect((err, db) => { // returns db connection
if (err != null) {
console.log(err)
return
}
client = db
})
Problem here is, I don't know how to implement this into my vue/quasar app. Do I have to save this as a component or can I access it directly using <script src="app.js">?
Is using the above code the correct way to connect to my mongoDB database or is there an easier way?

Related

NextJs + Mongoose + Mongo Atlas multiple connections even with caching

I am using NextJS to build an app. I am using MongoDB via mongoosejs to connect to my database hosted in mongoAtlas.
My database connection file looks like below
import mongoose from "mongoose";
const MONGO_URI =
process.env.NODE_ENV === "development"
? process.env.MONGO_URI_DEVELOPMENT
: process.env.MONGO_URI_PRODUCTION;
console.log(`Connecting to ${MONGO_URI}`);
const database_connection = async () => {
if (global.connection?.isConnected) {
console.log("reusing database connection")
return;
}
const database = await mongoose.connect(MONGO_URI, {
authSource: "admin",
useNewUrlParser: true
});
global.connection = { isConnected: database.connections[0].readyState }
console.log("new database connection created")
};
export default database_connection;
I have seen this MongoDB developer community thread and this GitHub thread.
The problem seems to happen only in dev mode(when you run yarn run dev). In the production version hosted on Vercel there seems to be no issue. I understand that in dev mode the server is restarted every time a change is saved so to cache a connection you need to use as global variable. As you can see above, I have done exactly that. The server even logs: reusing database connection, then in mongoAtlas it shows like 10 more connections opened.
How can I solve this issue or what am I doing wrong?

Can not connect Azure Functions to MongoDB (created in azure)

I have a MongoDB setup on azure, and I am tring to connect to it via azure function.
These are the steps I took:
Creating a Simple Azure Function
Installed the MongoDB Driver on Azure, To install the MongoDB Node.js driver, I went go to .scm.azurewebsites.net, and clicked on 'Debug Console' -> 'PowerShell'.
I Navigated to the D:\home\site\wwwroot directory and clicked on the plus icon to create a new file called package.json.
I Created and saved the below package.json file.
{
"name": "nameofunction",
"dependencies": {
"mongodb": "3.x"
}
}
Next, I ran npm install from the shell.
From the Azure Function I should be able to connect to MongoDB and execute a query using the below code.
const mongodb = require('mongodb');
const url = "mongodb://cosmod: <PASSWORD>==#cosmodb.documents.azure.com:10255/?ssl=true&replicaSet=globaldb";
module.exports = async function (context, req) {
mongodb.connect(url, function(error, client) {
if (error) throw error;
var dbo = client.db("mydb");
dbo.createCollection("customers", function(err, res) {
if (err) throw err;
context.log("Collection created!");
db.close();
});
});
};
My code is throwing up a Status: 500 Internal Server Error
The more I look at the code, the more i can not understand why this should not work.
The package-lock.jsonhas all the dependencies loaded after I ran npm install in the shell.
I appreciate any help in resolving this.
This seems weired, I also followed same and was able to connect to my db.
Can you please check your cosmos connectiondb, mongo compatible connection string? Are you able to connect that from other mongo clients
Status: 500 Internal Server Error
I assume that it dues to the code mongodb.connect(url, function(error, client)
please change the code to
mongodb.MongoClient.connect(uri, function(error, client)
A review of the Azure Cosmos DB account- Quick start documentation accessible via the Azure Cosmos DB account menu side blade; connecting the MongoDB app is among others via:
the Node.js 2.2 driver and
the Node.js 3.0 driver
I was using the Node.js 2.2 driver connection string in the azure function which is not compatible with the Node.js 3+ driver dependency in my app. Using the Node.js 3.0 driver connection string, I was able to connect the MongoDB app, without the error. The double equality sign in the password string is url encoded in the 3+ driver.
Node.js 3+ driver connection string
var mongoClient = require("mongodb").MongoClient;
mongoClient.connect("mongodb://cosmodb:<PWD>%3D%3D#cosmodb.documents.azure.com:10255/?ssl=true", function (err, client) {
client.close();
});
Node.js 2.2 driver connection string
var mongoClient = require("mongodb").MongoClient;
mongoClient.connect("mongodb://cosmodb:<PWD>==#cosmodb.documents.azure.com:10255/?ssl=true", function (err, db) {
db.close();
});

Meteor MongoDB database localhost vs server

I made a site with Meteor and it works perfectly on localhost, but not on the server. I have a collection in my mongoDB that I fill using a python-script, this script filled both my local as server mongoDB. I can see that it is filled using a database manager. However the call in my localhost prints an array with the documents, the same code/action on the website on the server returns an empty array.
I really have no clue what the problem could be, somebody that can help me figure this out?
python-script, same for local as on server!
def write_data(documents, collection_name):
# when running locally
#client = pymongo.MongoClient("localhost", 3001)
#db = client.meteor
# on the server
client = MongoClient(mongoURL, username=****,
password=****)
db = client.mercurius
if collection_name in db.collection_names():
collection = db[collection_name]
collection.drop()
new_collection = db[collection_name]
new_collection.insert_many(documents)
cursor = new_collection.find({})
for document in cursor:
print(document)
I use mup to setup the containers, it works for the other things, my code. mongoURL in mup is same as in this script above.
Meteor.call( 'getDBinfo', function(err, response){
console.log(response);
console.log(err);
});
The above call in meteor returns a full array(53 documents) on localhost, empty on server. Both have an undefined err.

check if mongodb DB exist at time of connection

I am new to mongodb and mongoose. I used the below code from internet. I never created the database MEANSTACK which i am referring below. When saving the record it never complained that DB or collection does not exist like it used to happen in MYSQL. How do i make sure that DB or collection exists before doing any operation and its not created automatically.
var mongoose = require( 'mongoose' );
var dbURI = 'mongodb://localhost/MEANSTACK';
mongoose.connect(dbURI);
mongoose.connection.on('connected', function () {
console.log(chalk.green('Mongoose connected to ' + dbURI));
});
mongoose.connection.on('error',function (err) {
console.log(chalk.red('Mongoose connection error: ' + err));
});
mongoose.connection.on('disconnected', function () {
console.log(chalk.red('Mongoose disconnected'));
});
Sry for writing my note as an answer. I can't write comments because my repo is below 50. When you run your server and it runs with no mistakes, that means your database var dbURI = 'mongodb://localhost/MEANSTACK' exists or at least your mongodb server is accessible. Otherwise you would get an error:
failed to connect to server [localhost:27017] on first connect
And additionally mongoose creates a db if it doesn't exist on server run, so it do the check you need on server run like "out of the box"

nodejs chat example does not work

I've come across a node chat example on github, When I try to run it, I see the following error:
Error connecting to mongo perhaps it isn't running ?
I've installed mongo 0.9.2, nodejs 5.2 pre, npm 3.0 and other dependencies. The example can be found here: https://github.com/gregstewart/chat.io
I can not determine whether if the example not really works or I didn't run it right. Please help.
Did you install and start mongo-db on your system? This error is mostly because of a missing mongo instance running on the local machine.
Check out the follwing code excerpts from chat.io.
main.js:
/**
* Configure the user provider (mongodB connection for user data storage)
*/
var userProvider = new UserProvider('localhost', 27017);
Creates a new UserProvider object using host and port for database (localhost:27017, mongo-db default).
UserProvider.js:
UserProvider = function(host, port) {
this.db = new mongo.Db('node-mongo-chat', new Server(host, port, {auto_reconnect: true}, {}));
this.db.addListener('error', function(error) {
console.log('Error connecting to mongo -- perhaps it isn\'t running?');
});
this.db.open(function() {
});
};
Opening the connection to the server, printing out an error on failure (the error you mentioned above).
Consider reading up on the mongo-db docs concerning installation and setup here