Hi,
I was trying to get the BSON document out of the MongoDB. As per the JSON And BSON | MongoDB , i am expecting the document received from the driver as :
{"hello": "world"} →
\x16\x00\x00\x00 // total document size
\x02 // 0x02 = type String
hello\x00 // field name
\x06\x00\x00\x00world\x00 // field value
\x00 // 0x00 = type EOO ('end of object')
But i see it in readable json format.
The data inserted in MongoDB:
db.test.insert({“hello”: “world”})
Sample code:
MongoClient mongoClient = MongoClients
.create("mongodb://localhost:27018/admin");
MongoDatabase database = mongoClient.getDatabase("MYTEST");
MongoCollection<Document> coll = database.getCollection("testarr");
Publisher<Document> publisher = coll.find();
MongoDBObservableSubscriber<Document> subscriber = null;
subscriber = new MongoDBObservableSubscriber<Document>();
publisher.subscribe(subscriber);
Queue<Document> mongoCursor = subscriber.getResults();
while (true) {
if (!mongoCursor.isEmpty()) {
Document document = mongoCursor.poll();
if (document != null) {
System.out.println("==========");
System.out.println("actual document -> "+document);
System.out.println("Bson document->"+document.toBsonDocument());
System.out.println("Json document->"+document.toJson(JsonWriterSettings.builder().outputMode(JsonMode.EXTENDED).build()));
RawBsonDocument raw = RawBsonDocument.parse(document.toJson(JsonWriterSettings.builder().outputMode(JsonMode.EXTENDED).build()));
System.out.println("raw document ->"+raw);
final BsonDocument ceDoc = document.toBsonDocument();
final OutputBuffer outputBuffer = new BasicOutputBuffer();
final BsonWriter innerWriter = new BsonBinaryWriter(outputBuffer);
BsonDocumentCodec bsonDocumentCodec= new BsonDocumentCodec();
bsonDocumentCodec.encode(innerWriter, ceDoc, EncoderContext.builder().build());
final BsonBinary encoded = new BsonBinary(outputBuffer.toByteArray());
System.out.println("Encoded->"+encoded.toString());
Bson bsonObject =BsonDocument.parse(document.toJson(JsonWriterSettings.builder().outputMode(JsonMode.EXTENDED).build()));
System.out.println("bsonObject->"+bsonObject);
System.out.println("==========");
}
}
}
output:
actual document -> Document{{_id=618b9c31759ba7a2fa73094c, hello=world}}
Bson document->{"_id": {"$oid": "618b9c31759ba7a2fa73094c"}, "hello": "world"}
Json document->{"_id": {"$oid": "618b9c31759ba7a2fa73094c"}, "hello": "world"}
raw document ->{"_id": {"$oid": "618b9c31759ba7a2fa73094c"}, "hello": "world"}
Encoded->BsonBinary{type=0, data=[39, 0, 0, 0, 7, 95, 105, 100, 0, 97, -117, -100, 49, 117, -101, -89, -94, -6, 115, 9, 76, 2, 104, 101, 108, 108, 111, 0, 6, 0, 0, 0, 119, 111, 114, 108, 100, 0, 0]}
bsonObject->{"_id": {"$oid": "618b9c31759ba7a2fa73094c"}, "hello": "world"}
In non of the ways, I see output in actual BSON format[as below one.]
\x16\x00\x00\x00 // total document size
\x02 // 0x02 = type String
hello\x00 // field name
\x06\x00\x00\x00world\x00 // field value
\x00 // 0x00 = type EOO ('end of object')
Is there are the way to derive the BSON format.
Thanks in advance.