Create Mongo DB database dynamically in authentication mode with Mongo Client - javascript

I have an application where the customer creates a company in UI. For every company, I need to create DB in authentication mode with MongoClient in NodeJS.

Databases can be created on the fly. MongoDB will create a database the first time you store data in it (such as when you create a collection in it). See https://docs.mongodb.com/manual/core/databases-and-collections/ for more details.
When using the MongoDB Node.js driver, you can indicate the name of the database and the collection when storing new data. If the database and collection do not exist, MongoDB will create them for you.
Example:
const result = await client.db("sample_airbnb").collection("listingsAndReviews").insertOne(newListing);
The name of the database is "sample_airbnb" and the name of the collection is "listingAndReviews". If those do not exist when this code is executed, MongoDB will create the database and collection. See https://developer.mongodb.com/quickstart/node-crud-tutorial for more examples.

Related

how we save logs of our application actions in mongodb(node.js) with one schema and limited information

I am working on a node js with MongoDB database we want to logs of our application action with limited information like success, action name, user id, time, etc so how we can do this in node js with MongoDB
You can look in the official website of mongo how to do it Mongo Logs, and if you want a custom one just create a new db/collection to put in your logs and send it as a regular request.

Firebase Auth & MongoDB Database

I am starting an app (JS / React) and I'd like to use Firebase Authentication system (because it's powerful), with a MongoDB Database (on MongoDB Atlas -Cloud-) for storing the data ( More efficient than Firestore && I use GraphQL on the backend). I still want to use Firebase auth, because I don't want to dive into the authentication rabbit hole for now...
Firebase generates an id for newly ceated users (sent in tokens), I want then to have a collection 'Users' on Mongo to store data about those users.
Here is my problem : The id generated by firebase auth doesn't fit the id syntax required by MongoDb Atlas, to fit the ObjectId type.
Here is an id generated by Firebase :
zOw5ERvHyucA3fPYlXlaa4SI1D23
and here is the error I get from mongo : 'must be a string of 24 hex characters'. Example of a working id :
5e7d4a383cae9a2218b45a55
I could use Stings as primarykey in Mongo but i'd lose the efficiency of ObjectIds.
For now I added a 'authId' attribute on my users, but I don't think this is optimal.
What do you think ? Any advice ?
Thanks !

Cannot create database with pouchdb in couchdb

while trying to just create a database with pouchDB for couchDB, the following error is thrown:
{"error":"not_found","reason":"Database does not exist."}.
My code to create a test db is as simple as this:
var db = new PouchDB('http://localhost:5984/testdb');
Also, the couchDB server is running and reachable at http://localhost:5984/ and it says:
{"couchdb":"Welcome","version":"2.1.1","features":["scheduler"],"vendor":{"name":"The Apache Software Foundation"}}
I have even enabled CORS for couchDB.
Thanks in advance.
According to the PouchDB API (especially the info block), it doesn't automatically create the database by creating a new PouchDB object.
You need to call an API function of the database in order to create the database.
For example, you could create your PouchDB object and then call db.info() to create the database.

Firebase Database query for http trigger

I'm trying to query my firebase realtime database for the list of User objects. I have a favourites field and in it I'm storing a list of id's of favourited users. How would I go about writing a http endpoint with cloud functions so that it returns a json list of User objects corresponding to these ids in database?
Thanks a lot.
You can configure your development environment for FCF by following this. Once you've initialized your project, write a function inside functions/index.js like this one:
exports.getFavouriteUsers = functions.https.onRequest((req, res) => {
var favouriteUsers = admin.database.ref('users/favourites').val();
res.status(200).send(favouriteUsers);
});
Finally deploy and you'll be able to make requests to the endpoint shown in the console.
Look at this documentation for HTTP triggers

Two connections with mongoose in the same application. The second connection is to connect the first dependent

I have a problem. I want to have two connections to the database. In the first database, I have the login information and the database name where are the other information. It is possible to do this using Node.js, mongoose and mongoDB?
You could do it as follows:
var mongoose1 = require('mongoose'),
mongoose2 = require('mongoose');
mongoose1.connect('mongodb://FirstIPAdrress:27017/firstDatabase');
//get values for the second database via find
mongoose2.connect('mongodb://SecondIPAdrress:27017/secondDatabase');

Categories

Resources