MongoDB setup db script (project based NodeJS) - node.js

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.)

Related

Moving specific collections from mongodb atlas to archive db

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);

How can i configure Mongoose to automatically create objects in my mongoDB on app start if none is found?

I am coming from a Java Hibernate frameworks, where an sql script can be written and added to a project, with the intentions of creating objects in my Mongo Database when the app runs and no data was found in a specific database table. I am using Mongoose presently with mongoDB, and i want to initialize my mongoDB table with objects if none is found. Initially i achieved this my creating an object and calling the new mongoose.save(obj) inside my app.js which is my start up file. But is there a cleaner way one already supported by Mongoose, or is this my best bet ?

MongoDB create DB from files

I am trying to install an app with nodeJS(totalJS) and MongoDB. When I run my nodeJS server it says that I have no db and I have to create it from a folder. How can I create a mongoDB from a folder of JSON files ?
I am not good at nodeJS so maybe it's my mongodb ling that is wrong ?
database : mongodb://127.0.0.1/data/db
Then I start my mongo server I use --dbpath /data/db parameters and I have copied JSON files into this folder but it's not reconized them.
Can you help me ?
/data/db is mongodb's default folder for storage, so in order to start your mongodb service you need to create one or specify an other one with --dbpath my/other/folder;
mongodb://127.0.0.1/data/db is not a good uri for connection, usually you will have something like mongodb://127.0.0.1/database where database is the name of your database.
But if you use some specific framework, do not know like sails.js then you need to specify it in the question, we can't help you otherwise.

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.

i18Next - NodeJS - How to change translations without reloading server

I am using i18next package for NodeJS to enable translation. I am using it as standard with a json file for each language-COUNTRY pair.
I'd like to build an admin page to edit translations without having to dig into the code; then, how can I "reload" the json files once edited by admin, without having to restart the server ?
Also, is it possible to use a DB (i am using mongodb) instead of JSON files ? Would it be more appropriate in this case?
I wish i knew the answer to how to reload the JSON files without completely re-initializing i18next-node.
In terms of storing your translations in a database, i18next-node can connect to MongoDB (as well as Redis and CouchDB): http://i18next.com/node/pages/doc_init.html#backend
You can set
i18next.sync.resStore = {}
and then call
i18next.init(callback)
to force a reload of your preloaded languages.
It is better to use an official API of i18n with reloadResources.
// reload all
i18next.reloadResources();
// reload languages
i18next.reloadResources(['de', 'fr']);
// reload namespaces for all languages
i18next.reloadResources(null, ['ns1', 'ns2']);
// reload namespaces in languages
i18next.reloadResources(['de', 'fr'], ['ns1', 'ns2']);

Resources