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.

Ajay Gupta
  • 3,192
  • 1
  • 22
  • 30
Abhishek bhutra
  • 1,400
  • 1
  • 11
  • 29

4 Answers4

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

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
swapnil
  • 101
  • 1
  • 3
1

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

Abhishek bhutra
  • 1,400
  • 1
  • 11
  • 29
0

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

Brendan W. McAdams
  • 9,379
  • 3
  • 41
  • 31
0

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

DeFOX
  • 86
  • 1
  • 8