[JAVA-2708] What are the differences between org.bson.BsonValue and classes defined under org.bson.types Created: 15/Dec/17  Updated: 28/Oct/23  Resolved: 15/Dec/17

Status: Closed
Project: Java Driver
Component/s: BSON
Affects Version/s: 3.5.0
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: jacky chen Assignee: Unassigned
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

mongo-java-driver-3.5.0.jar



 Description   

Hi Mongodb team,

I'm on data analysis in java application and found that for the data(field) type there are some similar definitions,
e.g.
org.bson.types.BSONTimestamp
org.bson.BsonTimestamp

Most of fields' type on java can be matched to classes defined under org.bson.types.* but only bson timestamp is exceptional, which is matched to org.bson.BsonTimestamp.

below is the results of <fieldValue>.getClass().getName(), which is printed by my application.
java.lang.String
org.bson.BsonTimestamp // never be org.bson.types.BSONTimestamp
org.bson.types.ObjectId
org.bson.Document
org.bson.types.MinKey
org.bson.BsonTimestamp
org.bson.types.ObjectId
org.bson.types.MaxKey
java.lang.String
org.bson.Document

it shows that field type comes from both subtype of org.bson.BsonValue and org.bson.types.*

What is the difference between them, or some duplicated definitions?

Can you please give me help or guide?
Thanks.



 Comments   
Comment by Ross Lawley [ 15/Dec/17 ]

Hi update0412,

Thanks for the ticket, 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<String, Object> 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<String, BsonValue>. 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.

For more information see the documents documentation.

Just to let you know for future reference, this project is for Java driver bugs or feature requests. The best place for questions regarding MongoDB usage or the Java driver specifics is the mongodb-user mailinglist or stackoverflow as you will reach a boarder audience there. If your business requires an answer from MongoDB within a time frame then we do offer production support.

If you do follow up via one of the options above please post a link and I will follow the conversation there.

All the best,

Ross

Comment by jacky chen [ 15/Dec/17 ]

mongos> db.chunks.find({_id:"test.users-id_MinKey"}).pretty()
{
"_id" : "test.users-id_MinKey",
"lastmod" : Timestamp(2, 0),
"lastmodEpoch" : ObjectId("5a331bff821258f0068b6372"),
"ns" : "test.users",
"min" : {
"id" :

{ "$minKey" : 1 }

},
"max" :

{ "id" : 1 }

,
"shard" : "replset1"
}
mongos>

"lastmod" : Timestamp(2, 0) is matched to org.bson.BsonTimestamp

Generated at Thu Feb 08 08:57:53 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.