2

Is it possible to update existing MongoDB collection with new data. I am using hadoop job to read write data to Mongo. Required scenario is :- Say first collection in Mongo is

{ 
  "_id" : 1,
  "value" : "aaa"
  "value2" : null
}

after reading data from Mongo and processing data, MongoDB should contain

{ 
  "_id" : 1,
  "value" : "aaa"
  "value2" : "bbb"
}

If possible, please provide some dummy code.

1
  • 1
    Can't you use a simple update with a $set basicDBObject? Commented May 30, 2012 at 14:40

4 Answers 4

3
BasicBSONObject query=new BasicBSONObject();
query.append("fieldname", value);
BasicBSONObject update=new BasicBSONObject();

update.append("$set", new BasicBSONObject().append("newfield",value));
MongoUpdateWritable muw=new MongoUpdateWritable(query,update,false,true);
contex.write(key, muw);

query : is used for providing condition(matching condition).

update : is used for adding new field and value in existing collection.

MongoUpdateWritable: 3rd parameter is upsert value(same as mongodb)

4th parameter is multiple update in many documents in a collection.

Set in Driver class job.setOutputValueClass(MongoUpdateWritable.class);

Sign up to request clarification or add additional context in comments.

Comments

1

I have done it by extending org.apache.hadoop.mapreduce.RecordWriter and overriding write method of this class.

Comments

0

The Mongo-Hadoop Connector doesn't currently suppor this feature. You can open a feature request in the MongoDB Jira if you like.

Comments

0

I have done it by the stratio, if you are using spark, you can check it out!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.