Moving specific collections from mongodb atlas to archive db - node.js

I did my homework before posting this question
So the case is that I want to create a utility in my nodejs application that will move specific collections from my main database to an archive database and vice versa. I am using mongo db atlas for my application. I have been doing my research and I found two possible ways one is to create a mongodump and store and other is to create a backup file myself using my node application and upload it to archive db. Using the later approach will cause to loose my collection indexes.
I am planning to use mongodump for the purpose but can't find a resource that shows how to achieve that. Any help would be appreciated. Also if any one has any experience with similar situation I am open to suggestions as well.

I recently created a mongodump & mongorestore wrapper for nodejs: node-mongotools
What does it mean?
you have to install mongo binary on your host by following official mongo documentation(example) and then, you could use node-mongotools to call them from nodeJS.
Here is an example but tool doc contains more details:
var mt = new MongoTools();
const dumpResult = await mt.mongodump({ uri, path })
.catch(console.log);

Related

node and express - how to use fake data

It's been a while since I used node and express and I was sure that this was possible, but i'm having an issue of figuring it out now.
I have a simple postgres database with sequelize. I am building a back end and don't have a populated database yet. I want to be able to provide fake data to use to build the front end and to test with. Is there a way to populate a database when the node server is started? Maybe by reading a json file into the database?
I know that I could point to this fake data using a setting in the environment file, but I don't see how to read in the data on startup. Is there a way to create a local database, read in the data, and point to that?
You can use fake factory package, I think it can solve your problem.
https://www.npmjs.com/package/faker-factory
FakerJs provides that solution.
import { faker } from '#faker-js/faker';
const randomName = faker.name.findName();
const randomEmail = faker.internet.email();
with the above, you can run a loop for loop to be specific to create
the desired data you may need for your project.
Also, check on the free web-API that provides fake or real data to workon

User specific database in MongoDB

I am currently working on an inventory management software in Node js and MongoDB. I am pretty new to MongoDB, having worked in Oracle and MySQL for most of my projects.
Is it possible to create a separate database schema for every client who uses my software, with each client having access only to his copy of the database schema and collections?
The equivalent of selecting data in Oracle database would be
Select * from User1.table,
Select * from User2.table etc
Also, if it were possible, how would it be implemented using a node js mongo db client like mongoose?
I looked at MongoDB documentation, but it talks mainly about adding users to a database for authorization.
I apologize if it seems like a silly question, but id appreciate it if someone could point me in the right direction for this.
Before starting to invest a lot of time in the development of your project, check out other possible approaches to the scenario that you are trying to build.
I did a quick search on SO and found some additional threads with similar scenarios:
MongoDB Database vs. Collection
MongoDB Web App - Database per User
Additional info about mongoose database creation
Whenever you call the connect method on the mongoose object, you are either connecting to an existing database or you are creating it in case it doesn't already exist.
You could have a function that allows you to pass in a name argument with the name and create databases programmatically:
function createDatabase(name) {
var conn_string = 'mongodb://localhost/';
if (typeof name == 'string') {
conn_string += name;
}else{
return false;
}
mongoose.connect(conn_string);
}
Also, be aware that a database will be created when you first insert a record in a collection of that particular database.
It is not sufficient to only connect to the database, you also have to insert a record.
As per my previous example, you could also pass a schema parameter to the function, tailored to each user's profile and fire an insert statement after you connect to that database.

How to preload MongoDB document?

I'm writing a web application with Node and MongoDB (with mongoose) - I have a collection that has a pre-defined data (images links that the users will choose one of them - they set of the images links is closed and won't be changed during the run of the application) and it need to be joined with other collection. I don't know if it is better to create a collection for this or to save the data in a single JSON file. But because i need to join this collection with other collection I decided to save the data in the DB.
Now the question is how I can create the data in the DB once and each time I raise the Node server the data won't be created again? (something like upsert)? and where is the best place to add this method? in the Schema module or where?
Thanks.
I found a way to do it by running an import command before raising the server (or even after the server is up) - I run the following line which will create the new documents or update the exist documents (according to the _id in the JSON file):
mongoimport --db test --collection supported_images --type json --file SupportedImages.json --upsert --jsonArray
Thanks everyone.

MongoDB setup db script (project based NodeJS)

I am setting up quite big project based on NodeJS and MongoDB. I though about some setup script for the database so that I could automate db cleaning, setting up collections, defining indexes, etc on updates. In classic SQL approach it's common to write *.sql file that could be run from shell... Is there some good equivalent to do it with Mongo?
I added NodeJS in the title because I also though it would be nice idea to add the script definition into "scripts" part in package.json file but it is not the main problem here. Also, if it helps I decided to use Mongoose as a main driver in the project...
In mongodb, the equivalent to a sql file is basically a js file.
See here for more info.
And just a note: mongoose is an odm, not a driver.
Update
Suppose we have the following script file
/**
* script.js
*/
db.createCollection('test');
You can execute it in mongo shell this way:
mongo 127.0.0.1:27017/dbname ./script.js
If you need to specify a username and password, you can use -u and -p switches. (Read this for more info.)

Getting started with a database for Node

The most relevant question here on StackOverflow was this, which still doesn't answer my question because the answer nor the redis repo give a tutorial/walk-through that beginners don't understand.
The thing is, I have absolutely no idea how to setup a simple database in order to create a simple to-do list or blog on my own. This is probably the closest tutorial on how to setup a database. But it's lacking in a sense due to not having a schema or so defined in order for me to edit or add "tables".
Simply put, I'm looking for a tutorial a complete beginner is able to follow on how to setup a database and define custom schemas for data (e.g. products that can have reviews nested in them).
Any suggestions?
I guess this should cover it: Node.js, MongoDB and Mongoose
You mentioned that you wanted to create a Todo application. There is TodoMVC. You can see various MVC Frameworks in action. There's also an example with Mongoose and Backbone.
For installing different NoSQL options including MongoDB, CochDB, Redis and SQLite for use with Node.js this is a nice walk through.
Once installed the following steps are required to get going with the database. Typical example for mongodb:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/your_database');
var Schema = mongoose.Schema;
var User = new Schema({
'_id' : String,
'name' : String,
'votes' : Number });
var User_Model = mongoose.model('User', User);
Approaches to read/ write data from/to the database varies.
I think the simplest thing to do is to download and install MongoDB and use the mongodb-native driver to store and access your data.
MongoDB is schema-less so you won't need to define any table or keys in advance. Simply open a collection (which will be created automatically if it doesn't exist) and start storing documents/objects in it.
Mongo is fast, powerful and, in my opinion, easy to use.
See mongodb.org for more information.
I find this screencasts very helpful for nodeJS + MongoDB, even though the mongoose has been updated a lot since the video, but the basics remained. And you'll learn the new one in no time just by skimming mongoose's doc.
I've downloaded all of his videos and watch them every time I need brushing up my node skillz :p
Sidenote: He just uploaded screencast for couchDB, for other who prefers different cup of tea.

Resources