JSON documents
JSON documents consist of fields, which are name-value pair objects. The fields can be in any order, and be nested or arranged in arrays.
There is no enforcement of document structures. Other documents in the same collection might therefore have a subset of these fields, extra fields, or different representations of the same field.
The field name is always of String type and serves as key for the
field, and, therefore, it must be unique. The values can be any of
the supported JSON data types. The first six data types, from java.lang.String to java.util.Date,
are recognized by JSON. The types that follow are extensions that
can be stored in the JSON store, but need to be represented externally
with the MongoDB-defined functions, or in a Java™ language construct.
Data type | Example in JSON format |
---|---|
java.lang.String | "string" |
java.lang.Integer | 3 |
java.lang.Long | 4294967296 |
java.lang.Double | 6.2 |
java.Lang.Boolean | true / false |
java.lang.Byte [] | { "$binary": "(base64-encoded value)", "$type": "0" } |
java.util.Date (millisecond precision, in Coordinated Universal Time) | { "$date" : "1998-03-11T17:14:12.456Z" } |
java.util.regex.Pattern | { "$regex" : "ab*" , "$options" : "" } |
java.util.UUID | { "$uuid" : "fb46f9a6-13df-41a2-a4e3-c77e85e747dd" } |
com.ibm.nosql.bson.types.ObjectId | { "$oid" : "51d2f200eefac17ea91d6831" } |
com.ibm.nosql.bson.types.Code | { "$code" : "mycode" } |
com.ibm.nosql.bson.types.CodeWScope | { "$code" : "i=i+1", "$scope" : {} } |
com.ibm.nosql.json.api.BasicDBObject | { "a" : 1, "b": { "c" : 2 } } |
com.ibm.nosql.json.api.BasicDBList | [1 , 2, "3", "abc", 5] |
For Date
string values, the client converts the value to Coordinated Universal
Time to be stored in Db2® databases. It is also
retrieved as a Date
in Coordinated Universal Time format.
The following example represents a sample JSON document
with array values and nested objects:
9
{
name:"Joe",
age:25,
phone:["555-666-7777", "444-789-1234"],
homeAddress:
{street:"Sycamore Avenue",
city: “Gilroy”,
zipcode:"95046"
}
businessAddress:
{
street:"Bailey Avenue",
City: “San Jose”,
zipcode:"95141"
}
}