I need to insert documents on MongoDB (with specific id instead of auto generated ObjectID) using java..
To insert one document or update if exist, I tried
Try this @ebt_dev:
db.collection("collectionname").insertOne(data, { forceServerObjectId: false })
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.