Reputation: 251
Can anyone please help me understand the difference between
Document
BasicDBObject
BsonDocument
BasicDBList
used in mongo driver APIs .
I was referring the following document http://mongodb.github.io/mongo-java-driver/3.2/driver/reference/crud/ how ever could not really understand in which scenarios they must be used. Any suggestion ?
Upvotes: 5
Views: 4027
Reputation: 935
A little late, but, as mentioned here - https://jira.mongodb.org/browse/JAVA-2708
the BSON spec supports various BSON types. The org.bson.types package contains classes implementing various BSON types for use with Java. These are the types where there is no Java equivalent (eg. MaxKey).
If you were to use the org.bson.Document type to represent BSON documents, then typically the _id field will contain an org.bson.types.ObjectId value. The Document class is essentially a Map and values can be of any type. When encoding them to BSON, the values have to be mapped to a BSON type (this is done via Codecs and the CodecRegistry).
Some users wanted a type-safe BSON document implementation, this is where BsonDocument comes in as it essentially is a Map. Only implementations of the BsonValue class can be stored in a BsonDocument, making it type-safe and complete. Most users will prefer either the Document or POJOs as they are easier to use.
Upvotes: 4