MongoDB updates data using script

Is it possible to update data in mongodb using some kind of script? I don't want (can't) access the mongo shell, but would like to fulfill the mongoshell update requests. My data is a csv file. I use hadoop for data analysis (extraction and transformation). I need to return data to mongodb and update some attributes. As an update link, I would like to use the generated id

Can this task be completed?

any help would be much appreciated

+6
source share
2 answers

Do you want to read data from a CSV file and import into mongodb? You can create a script file (javascript) and use the mongo shell to execute it, as described in the " shell script ".

Sample session, test database, starting with an empty foo collection:

> db.foo.insert({name : "james", position : "forward"}) > db.foo.find() { "_id" : ObjectId("4f0c99f6cb435f1e7d7fedea"), "name" : "james", "position" : "forward" } > 

then you create your script let say mongo_scripting.js:

 db.foo.insert({name : "wade", position : "guard"}); db.foo.update({name : "james"}, {$set : {position : "power forward"}}, false, true); 

and run the script:

 mongo localhost:27017/test mongo_scripting.js 

Return to Mongo:

 > db.foo.find() { "_id" : ObjectId("4f0c99f6cb435f1e7d7fedea"), "name" : "james", "position" : "power forward" } { "_id" : ObjectId("4f0c9a64a4a4642bae6459ea"), "name" : "wade", "position" : "guard" } > 

you see that one document has been updated and one new inserted.

An alternative is to use the java / python driver ... to load data.

+10
source

if you can connect to MongoDB at all, then you can use the shell with confidence. Just run the shell on the local machine and tell it to connect to the remote Mongo instance, for example:

mongo dbserver.mydomain.com/foo

You can also use mongoimport, http://www.mongodb.org/display/DOCS/Import+Export+Tools , although mongoimport will want to create or replace entire documents, rather than updating fields in documents as you asked.

It seems to me that you need to write a script to process each CSV line and update documents in MongoDB. In Python, this script will look something like this:

 import csv, pymongo, sys foo_db = pymongo.Connection("dbserver.mydomain.com").foo csv_reader = csv.reader(open(sys.argv[1], 'rb'), delimiter=',', quotechar='"') for line in csv_reader: _id, field1, field2 = line foo_db.my_collection.update({ "_id": _id }, { "$set": { "field1": field1, "field2": field2 } }, safe=True) 
+2
source

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


All Articles