7

I need to insert documents on MongoDB (with specific id instead of auto generated ObjectID) using java..

  1. To insert one document or update if exist, I tried use findOne to search for the id, if it doesn't exist then insert the id and then findAndModify. It works but I don't feel that it's efficient way, it's time consuming. Is there a better way to achieve that?

  2. To insert multiple documents at once,I'm following this solution. but I don't know how I can insert my custom Id instead of objectID?

Any help will be appreciated

Community
  • 1
  • 1
ebt_dev
  • 149
  • 1
  • 3
  • 12

2 Answers2

8

For your first problem MongoDB has upsert so

db.collection.update(
   {query for id},
   {document},
   {upsert: true}
)

or in the Java driver

yourCollection.update(searchObject, modifiedObject, true, false);

If you want to set a custom ID you just set the _id key manually i.e.

yourBasicDBObject.put("_id",yourCustomId) 

you just have to ensure it is unique for each document.

You will also need to set the _id in your modifiedObject otherwise a new one will be generated.

As for the bulk operations, just setting a custom ID for each document by giving the _id key should also work.

Chris Kobrzak
  • 1,044
  • 14
  • 20
Trudbert
  • 3,178
  • 14
  • 14
  • True. But really that doesn't differ from the `.findAndModify()` approach which is pointed to already being taken? Forgive me if I am wrong, but the question seems to be asking "how to assign a custom _id value, in **bulk**. So Upserts are just the same, and clearly "multi" updates will not cut it for assigning a custom id. – Neil Lunn Oct 09 '14 at 09:04
  • @NeilLunn I understood the first part to be related to single documents since it uses `findOne()` to see if the _id is there, insert it just containing the `_id` if it's not and then `.findAndModify` it with content. `uspert` is the operation that does this in one step so easier. Question 2 only relates to insert so the setting an `_id` for each document part shoud be the solution. Specified that again – Trudbert Oct 09 '14 at 09:32
  • You seem to misunderstand the critique here. The problem pointed out by the OP is "what value" to assign to `_id`. For example, a "multi" update would just assign the default `ObjectId` value. But of course just "looping" inserts and getting your own id value and then contacting the server and updating and so on is "no efficient", which is what the OP says. Therefore the question really asks "What is efficient?" IMHO, and though maybe not worded in the best way. Ce la vie. – Neil Lunn Oct 09 '14 at 09:40
  • @NeilLunn Yep the question is not that clear and there are two questions the way I understand it: 1. starts with "To insert one document or update if exist" and asks for a better way to do it for a single document hence upsert for the single document 2. asks a different question. The method (bulk in- or upserts) is given but the question is how to assign an _id. Shouldn't a bulk upsert (like in the solution the op linked to) achieve this if an `_id` key is specified for each document (never realy needed bulk in- or upserts)? – Trudbert Oct 09 '14 at 09:52
  • @Trudbert I'don't understand your solution for my sec. problem. How I can set custom _id for multiple documents!! Thanks. – ebt_dev Oct 10 '14 at 13:55
  • @ebt_dev how do you generate your documents or where are they coming from? – Trudbert Oct 10 '14 at 14:13
  • @Trudbert I'm currently generating random values for testing my application. _The _id can be String or long_. I've two methods **putandUpdate** and **putAll**. In putandUpdate, I follow the steps I mentions in my question, but it's not efficient because I need to insert _id, find it and then findAndModify document . For putAll, I don't know how I can insert multiple documents at once. – ebt_dev Oct 10 '14 at 14:53
3

Try this @ebt_dev:

db.collection("collectionname").insertOne(data, { forceServerObjectId: false })
bhaRATh
  • 716
  • 4
  • 23