In terms of what you are trying to do, you are trying to add some kind of custom data type (in this case your POJO), but what you need to keep in mind is that the fields in the documents can only accept certain data types , not directly.
If you did not already know, Mongo Documents are structured in the same way as json. Therefore, you must explicitly create documents by creating fields and inserting values into them. There are certain data types that are allowed in value fields:
http://mongodb.imtqy.com/mongo-java-driver/3.0/bson/documents/
To help in your case, the code below takes POJO as a parameter and knowing the structure of the POJO, returns a Mongo Document that can be inserted into your collection:
private Document pojoToDoc(Pojo pojo){ Document doc = new Document(); doc.put("Name",pojo.getName()); doc.put("Surname",pojo.getSurname()); doc.put("id",pojo.getId()); return doc; }
This should work for insertion. If you want to index one of the fields:
database.getCollection("Records").createIndex(new Document("id", 1));
I hope this answers your question and works for you.
source share