Questions tagged [bson]

BSON stands for "Binary JSON". It is a computer data interchange format used mainly as a data storage and network transfer format in the MongoDB database.

BSON [bee · sahn], short for Bin­ary JSON, is a bin­ary-en­coded seri­al­iz­a­tion of JSON-like doc­u­ments. Like JSON, BSON sup­ports the em­bed­ding of doc­u­ments and ar­rays with­in oth­er doc­u­ments and ar­rays. BSON also con­tains ex­ten­sions that al­low rep­res­ent­a­tion of data types that are not part of the JSON spec. For ex­ample, BSON has a Date type and a BinData type.

BSON can be com­pared to bin­ary inter­change for­mats, like Proto­col Buf­fers. BSON is more "schema-less" than Proto­col Buf­fers, which can give it an ad­vant­age in flex­ib­il­ity but also a slight dis­ad­vant­age in space ef­fi­ciency (BSON has over­head for field names with­in the seri­al­ized data).

BSON was de­signed to have the fol­low­ing three char­ac­ter­ist­ics:

  1. Lightweight

    Keep­ing spa­tial over­head to a min­im­um is im­port­ant for any data rep­res­ent­a­tion format, es­pe­cially when used over the net­work.

  2. Traversable

    BSON is de­signed to be tra­versed eas­ily. This is a vi­tal prop­erty in its role as the primary data rep­res­ent­a­tion for Mon­goDB.

  3. Efficient

    En­cod­ing data to BSON and de­cod­ing from BSON can be per­formed very quickly in most lan­guages due to the use of C data types.

Related Links

1403 questions
12
votes
5 answers

Bson array (de)serialization with Json.NET

I am simply trying to serialize and deserialize a string array in Bson format using Json.NET, but the following code fails: var jsonSerializer = new JsonSerializer(); var array = new string [] { "A", "B" }; // Serialization byte[] bytes; using (var…
Xavier Poinas
  • 19,377
  • 14
  • 63
  • 95
12
votes
3 answers

am I exposing sensitive data if I put a bson ID in a url?

Say I have a Products array in my Mongodb. I'd like users to be able to see each product on their own page: http://www.mysite.com/product/12345/Widget-Wodget. Since each Product doesn't have an incremental integer ID (12345) but instead it has a…
jcollum
  • 43,623
  • 55
  • 191
  • 321
12
votes
1 answer

BSON | terminal and non-terminal

Reading through the BSON specification I came across the terminal and non-terminal terms in it. For example: Valid BSON data is represented by the document non-terminal. <...> The following basic types are used as terminals in the rest of the…
Shamaoke
  • 6,222
  • 8
  • 34
  • 40
11
votes
1 answer

not sure how to use ElemMatch in c# for MongoDb (newest driver version)

I have a MongoDB collection in the following format: { "_id" : ObjectId("5692a3397d7518330416f8e5"), "supertagname" : "xxx", "inclusions" : [ "test", "blabla" ], "exclusions" : [ ] } and I am trying to query…
chrisb
  • 323
  • 3
  • 13
10
votes
2 answers

Unable to deserialize PyMongo ObjectId from JSON

I'm seemingly unable to deserialize my MongoDB JSON document with the BSON json_util. The json.loads function is choking on the ObjectId() string. I had understood json_util capable of handling MongoDB's ObjectId format and transforming into usable…
jdev
  • 729
  • 2
  • 13
  • 36
10
votes
3 answers

How to ignore null values globally by calling obj.ToBsonDocument() using MongoDB C# driver?

Prehistory: I have a class hierarchy (a big one) which was generated from an XML schema (XSD). MyObj obj; Nowadays: I need to parse a string value, which is actually an XML to an object using the generated class hierarchy and then I need to put…
Antipod
  • 1,593
  • 3
  • 23
  • 43
10
votes
2 answers

Insert JSON into an existing MongoDB collection

I understood my mistake :) Thanks guys. I have one more Question, suppose i have multiple documents with the below structure in "Customer" collection. { "customerId":100, "FirstName":"xyz", "lastname":"pqr", "address":[ { …
darsh
  • 109
  • 1
  • 1
  • 7
10
votes
2 answers

How to save Timestamp type value in MongoDb | Java

From Java driver, I want to save a document that looks like below json in MongoDb { "ts" : Timestamp(1421006159, 4)} Options I tried. Option 1: Map doc= new HashMap(1); doc.put("ts", new BSONTimeStamp()); It results in the below not required…
hellojava
  • 4,904
  • 9
  • 30
  • 38
10
votes
2 answers

Bash - Convert MONGODB BSON to JSON

I have a shell command that calls a MongoDB command and produces a BSON output. I then want to parse that output with jq so I need to convert the BSON to JSON using jq's tojson. echo "db._adminCommand({replSetGetStatus : 1})" | /path/to/mongo How…
SnazzyBootMan
  • 669
  • 2
  • 15
  • 30
10
votes
1 answer

How to query date range on the MongoDB collection where the ISO date is stored in string field?

Scenario: Consider I am having a collection called MyCollection, with following data: { "_id" : 'MyUniqueID_01' "CreatedTime" : "2013-12-01T14:35:00Z", "LastModifiedTime" : "2013-12-01T13:25:00Z" } Now I want to query the MongoDB…
Amol M Kulkarni
  • 21,143
  • 34
  • 120
  • 164
10
votes
1 answer

Can't connect to MongoDB 2.0.5 database with pymongo 2.2

I've been stuck with diagnosing this for a few hours now and thought I'd see if any pymongo experts out there have any ideas: The following line of code: connection = pymongo.Connection('localhost', 27017) Generates the following…
SeaTurtle
  • 493
  • 1
  • 3
  • 13
9
votes
0 answers

Error returning vector of document objects using pyo3

Returning Vector of document objects from rust to python fails. I have a struct and method implementation in rust as follows. use mongodb::{ bson::{Bson, Document}, error::Error, sync::Client, }; use pyo3::prelude::*; #[pyclass] pub…
9
votes
2 answers

How do I serialize chrono::DateTime fields as ISODate when using the Rust Mongo driver prototype?

Datetime fields in structs are being serialized to Strings instead of ISODates when using the Rust Mongo driver prototype. How do I get the fields to be saved as ISODate? use chrono::{DateTime, Utc}; use mongodb::oid::ObjectId; use mongodb::{ …
nngg
  • 188
  • 1
  • 6
9
votes
2 answers

Python2: Installing json_util

I used to import json_util from bson: from bson import json_util Now I get: ImportError: cannot import name json_util How can I install json_util now?
user1581390
  • 1,900
  • 5
  • 25
  • 38
9
votes
2 answers

How to write sequence of Bson to file with MongoDB Java Driver

Using the MongoDB Java Driver library is there a way to stream writes of bson objects to a file and then later stream reads of bson objects from that file. Looking at the documentation I see nothing about how to encode a sequence of bson objects to…
user782220
  • 10,677
  • 21
  • 72
  • 135