Read the file from the mongo shell

I have a database called wordlists and a collection called rockyou.

I have a rockyou file with the contents:

123456 12345 123456789 password iloveyou princess 1234567 rockyou 12345678 abc123 

What I would like to do is upload the entire contents of the rockyou file to the rockyou collection in my database. I tried to find google, but the resources seem slim for mongo. I could really use some help with this, as well as maybe some good resources for this new DB application for me.

Thanks!

** EDIT * Here is the command I used and some of what I tried.

 use wordlists db.createCollection("rockyou") db.rockyou.insert(copyFile(~/wordlists/rockyou)) 

I tried some other methods to insert, but now I understand that even more than this ...

+5
source share
1 answer

If you really want to use only mongoshell, you can use the cat () command and do the following (txt is not required, this is exactly how my file was named):

 use wordlists var file = cat('path/to/yourFile.txt'); // read the file var words = file.split('\n'); // create an array of words for (var i = 0, l = words.length; i < l; i++){ // for every word insert it in the collection db.rockyou.insert({'word': words[i]}); } 

This was tested on Mongo 3.0.1 and produced something like:

 { "_id" : ObjectId("551491ee909f1a779b467cca"), "word" : "123456" } { "_id" : ObjectId("551491ee909f1a779b467ccb"), "word" : "12345" } ... { "_id" : ObjectId("551491ee909f1a779b467cd3"), "word" : "abc123" } 

But I would apply the logic here (e.g. with python):

 import pymongo connection = pymongo.Connection() collection = connection.wordlists.rockyou with open('path/to/yourFile.txt') as f: for word in f.readlines(): collection.insert({'word': word.rstrip()}) 
+14
source

Source: https://habr.com/ru/post/1216234/


All Articles