|
| 1 | ++++ |
| 2 | +date = "2015-03-19T14:27:51-04:00" |
| 3 | +title = "Documents" |
| 4 | +[menu.main] |
| 5 | + parent = "BSON" |
| 6 | + weight = 50 |
| 7 | + pre = "<i class='fa'></i>" |
| 8 | ++++ |
| 9 | + |
| 10 | +## Documents |
| 11 | + |
| 12 | +The driver includes several classes and interfaces used for representing BSON documents. |
| 13 | + |
| 14 | +### BsonDocument |
| 15 | + |
| 16 | +Although generally not needed by users of the high-level driver API, the [BsonDocument]({{< apiref "org/bson/BsonDocument" >}}) class is |
| 17 | +central to the way that documents are managed internally by the driver. The BsonDocument class can represent dynamically structured |
| 18 | +documents of any complexity with a type-safe API. For instance, the document `{ a: "MongoDB", b: [ { c: 1 } ] }` can be constructed as a |
| 19 | +BsonDocument as follows: |
| 20 | + |
| 21 | +```java |
| 22 | +new BsonDocument().append("a", new BsonString("MongoDB")) |
| 23 | + .append("b", new BsonArray(Arrays.asList(new BsonInt32(1), new BsonInt32(2)))); |
| 24 | +``` |
| 25 | + |
| 26 | +The type safety comes from BsonDocument implementing `Map<String, BsonValue>`, so even built-in types like `int`, `String` and `List` must |
| 27 | +be wrapped in a sub-class of `BsonValue`. For a complete list of BsonValue sub-types, please consult the |
| 28 | +[API documentation]({{< apiref "org/bson/BsonValue" >}}). |
| 29 | + |
| 30 | +### Document |
| 31 | + |
| 32 | +Most applications will use the [Document]({{< apiref "org/bson/Document" >}}) class instead. Like BsonDocument, the |
| 33 | +Document class can represent dynamically structured documents of any complexity; however, the typing is much looser, as Document |
| 34 | +implements `Map<String, Object>`. As a result, the same document as above can be constructed using the Document class as follows: |
| 35 | + |
| 36 | +```java |
| 37 | +new Document().append("a", "MongoDB") |
| 38 | + .append("b", Arrays.asList(1, 2)); |
| 39 | +``` |
| 40 | + |
| 41 | +There is less code to write, but runtime errors are possible if you inadvertently add an instance of an unsupported value type. The most |
| 42 | +commonly used value types are: |
| 43 | + |
| 44 | +| BSON type | Java type | |
| 45 | +|-----------|-------------------------| |
| 46 | +| Document | org.bson.Document | |
| 47 | +| Array | java.util.List | |
| 48 | +| Date | java.util.Date | |
| 49 | +| Boolean | java.lang.Boolean | |
| 50 | +| Double | java.lang.Double | |
| 51 | +| Int32 | java.lang.Integer | |
| 52 | +| Int64 | java.lang.Long | |
| 53 | +| String | java.lang.String | |
| 54 | +| Binary | org.bson.types.Binary | |
| 55 | +| ObjectId | org.bson.types.ObjectId | |
| 56 | +| Null | null | |
| 57 | + |
| 58 | +It is actually possible to change these mappings; the mechanism for doing so is currently beyond the scope of this reference. |
| 59 | + |
| 60 | +### DBObject |
| 61 | + |
| 62 | +Although not recommended for new applications, those upgrading from the 2.x driver series may continue to use the |
| 63 | +[DBObject]({{< apiref "com/mongodb/DBObject" >}}) interface to represent BSON documents. DBObject is similar to Document in that it |
| 64 | +represents BSON values as `Object`, but it has a few shortcomings that were impossible to overcome: |
| 65 | + |
| 66 | +- it is an interface rather than a class, so it's API can not be extended without breaking binary compatibility |
| 67 | +- it doesn't actually implement `Map<String, Object>` |
| 68 | +- because it is an interface, a separate concrete class called [BasicDBObject]({{< apiref "com/mongodb/BasicDBObject" >}}) which implements |
| 69 | +that interface, is required |
| 70 | + |
| 71 | +### Bson |
| 72 | + |
| 73 | +To tie these all together, the driver contains a small but powerful interface called [Bson]({{< apiref "org/bson/conversions/Bson" >}}). |
| 74 | +Any class that represents a BSON document, whether included in the driver itself or from a third party, can implement this interface and |
| 75 | +can then be used any place in the high-level API where a BSON document is required. The three classes discussed above all implement this |
| 76 | +interface and so can be used interchangeably based on the needs of a given application. For example: |
| 77 | + |
| 78 | +```java |
| 79 | +collection.find(new BsonDocument("x", new BsonInt32(1))); |
| 80 | +collection.find(new Document("x", 1)); |
| 81 | +collection.find(new BasicDBObject("x", 1)); |
| 82 | +``` |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
0 commit comments