0

I am using mongoOperations of Spring Data mongodb to store in MongoDB// mongoOperations.save(reqObj);

I need to save the same JSON document which i am getting as rest API response in two collections.

@Document(collection="collection_a")
public class Response {
}

I am able to save in collection collection_a. I also need to save the same json in another collection collection_b.

Should I create another class like below and copy the value from Response to ResponseCopy? or is there better approach.

@Document(collection="collection_b")
public class ResponseCopy {
}
user1346346
  • 195
  • 2
  • 16

1 Answers1

0

At present there is no possibility to do that. You may need to maintain two classes for two documents.

the only solution could be looping and using $lookup as mentioned in their doc: https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/

db.collection('collection_a').findAndModify(
            { "_id": ObjectId(req.body.id) },
            [],
            { $set: { "delete": req.body.delete } },
            { new: true },
            function (err, data123) {
                if (err) throw err;
                db.collection('collection_a').findAndModify(
                    { "_id": ObjectId(req.body.error_id) },
                    [],
                    { $set: { "read": true } },
                    { new: true },
                    function (err, data1234) {
                        if (err) throw err;

                        res.write(JSON.stringify(data1234.value));
                        res.end();
                        db.close();
                    });
            });
Mebin Joe
  • 2,172
  • 4
  • 16
  • 22
  • I have done this by copy the value from Response to ResponseCopy by orika mapper and saved in the collections separately. – user1346346 Feb 04 '19 at 12:11