How to seed a .js file using mongoid in Rails - node.js

I am trying out Mongoid in rails4.
I need to seed a .js file in the Mongoid database. How is that done? I have a .js file in my db/ folder but can't figure out how to seed it...

I had the same issue a while back, but was using MongoMapper not Mongoid.
I was able to seed the mongo database from a .js file using this command:
mongo [database_name] [seed_file_path]
ex: mongo my_db db/seed.js
I found the database name in the mongo.yml file which was created by MongoMapper. I think it would be mongoid.yml with Mongoid.
Hope this helps.

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

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.

I have my log file path in the database how to pass that in logstash?

How to pass filepath which is there in DB to configure lagstash.
I can get the file path using input jdbc plugin from database it works. But how to pass the file path from there.
Logstash reads a static config file. There was talk of an API in logstash2 that seemingly could have provided what you were looking for, but it has been pushed back to a future release.

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

Resources