mongodb c# how to work with BSON document

There are a few ways, but here’s one: // build some test data BsonArray dataFields = new BsonArray { new BsonDocument { { “ID” , ObjectId.GenerateNewId()}, { “NAME”, “ID”}, {“TYPE”, “Text”} } }; BsonDocument nested = new BsonDocument { { “name”, “John Doe” }, { “fields”, dataFields }, { “address”, new BsonDocument { { “street”, … Read more

MongoDB Structure for message app

I see that this question is old, but for anyone interested, a similar question was asked and one answer looks viable https://stackoverflow.com/a/30830429/132610 Conversation : { id: 123, members: [ user_id1, user_id2 ] } Message { conversationId: 123, author: user_2, body: ‘Hi what’s up’ } Message { conversationId: 123, author: user_1, body: ‘Whanna ask some question … Read more

Which one is lighter, JSON or BSON?

From the BSON FAQ: BSON is designed to be efficient in space, but in many cases is not much more efficient than JSON. In some cases BSON uses even more space than JSON. The reason for this is another of the BSON design goals: traversability. BSON adds some “extra” information to documents, like length prefixes, … Read more

Compare JSON and BSON

The efficiency of JSON vs BSON depends on the size of the integers you’re storing. There’s an interesting point where ASCII takes fewer bytes than actually storing integer types. 64-bit integers, which is how it appears your BSON document, take up 8 bytes. Your numbers are all less than 10,000, which means you could store … Read more

MongoDB – What about Decimal type of value?

If you want an exact representation for financial purposes, then doubles or floating point values are unsuitable as the fractional parts are subject to rounding error. Certain decimal values cannot not be represented using binary-based floating points and must be approximated. For a less technical intro, see The trouble with rounding floating point numbers; if … Read more

How to convert BSON to JSON with human-readable date format

bsondump converts BSON files into human-readable formats, including JSON. For example, bsondump is useful for reading the output files generated by mongodump. Source: https://docs.mongodb.com/manual/reference/program/bsondump Examples bsondump –outFile collection.json collection.bson The –pretty option outputs documents in a pretty-printed format JSON, eg: bsondump –pretty –outFile collection.json collection.bson

Force mongodb to output strict JSON

The MongoDB shell speaks Javascript, so the answer is simple: use JSON.stringify(). If your command is db.serverStatus(), then you can simply do this: JSON.stringify(db.serverStatus()) This won’t output the proper “strict mode” representation of each of the fields ({ “floatApprox”: <number> } instead of { “$numberLong”: “<number>” }), but if what you care about is getting … Read more

Performant Entity Serialization: BSON vs MessagePack (vs JSON)

// Please note that I’m author of MessagePack. This answer may be biased. Format design Compatibility with JSON In spite of its name, BSON’s compatibility with JSON is not so good compared with MessagePack. BSON has special types like “ObjectId”, “Min key”, “UUID” or “MD5” (I think these types are required by MongoDB). These types … Read more