Package com.mongodb
Class BasicDBObject
- java.lang.Object
 - 
- java.util.AbstractMap<K,V>
 - 
- java.util.HashMap<K,V>
 - 
- java.util.LinkedHashMap<String,Object>
 - 
- org.bson.BasicBSONObject
 - 
- com.mongodb.BasicDBObject
 
 
 
 
 
 
- 
- All Implemented Interfaces:
 DBObject,Serializable,Cloneable,Map<String,Object>,BSONObject,Bson
public class BasicDBObject extends BasicBSONObject implements DBObject, Bson
A basic implementation of BSON object that is MongoDB specific. ADBObjectcan be created as follows, using this class:DBObject obj = new BasicDBObject(); obj.put( "foo", "bar" );
- See Also:
 - Serialized Form
 - MongoDB documentation
 - MongoDB Documents
 
 
- 
- 
Nested Class Summary
- 
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,V extends Object> 
 - 
 
- 
Field Summary
- 
Fields inherited from interface org.bson.conversions.Bson
DEFAULT_CODEC_REGISTRY 
 - 
 
- 
Constructor Summary
Constructors Constructor Description BasicDBObject()Creates an empty object.BasicDBObject(int size)Creates an empty objectBasicDBObject(String key, Object value)Creates an object with the given key/valueBasicDBObject(Map map)Creates an object from a map. 
- 
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description BasicDBObjectappend(String key, Object val)Add a key/value pair to this objectObjectcopy()Creates a new instance which is a copy of this BasicDBObject.booleanequals(Object o)inthashCode()booleanisPartialObject()WhethermarkAsPartialObject()was ever called only matters if you are going to upsert and do not want to risk losing fields.voidmarkAsPartialObject()If this object was retrieved with only some fields (using a field filter) this method will be called to mark it as such.static BasicDBObjectparse(String json)Parses a string in MongoDB Extended JSON format to aBasicDBObject.static BasicDBObjectparse(String json, Decoder<BasicDBObject> decoder)Parses a string in MongoDB Extended JSON format to aBasicDBObject.<TDocument>
BsonDocumenttoBsonDocument(Class<TDocument> documentClass, CodecRegistry codecRegistry)StringtoJson()Gets a JSON representation of this document using theJsonMode.RELAXEDoutput mode, and otherwise the default settings ofJsonWriterSettings.BuilderandDBObjectCodec.StringtoJson(Encoder<BasicDBObject> encoder)Gets a JSON representation of this documentStringtoJson(JsonWriterSettings writerSettings)Gets a JSON representation of this documentStringtoJson(JsonWriterSettings writerSettings, Encoder<BasicDBObject> encoder)Gets a JSON representation of this documentStringtoString()Returns a JSON serialization of this object- 
Methods inherited from class org.bson.BasicBSONObject
containsField, get, getBoolean, getBoolean, getDate, getDate, getDouble, getDouble, getInt, getInt, getLong, getLong, getObjectId, getObjectId, getString, getString, putAll, putAll, removeField, toMap 
- 
Methods inherited from class java.util.LinkedHashMap
clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, removeEldestEntry, replaceAll, values 
- 
Methods inherited from class java.util.HashMap
clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, put, putIfAbsent, remove, remove, replace, replace, size 
- 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait 
- 
Methods inherited from interface org.bson.conversions.Bson
toBsonDocument 
- 
Methods inherited from interface org.bson.BSONObject
containsField, get, keySet, put, putAll, putAll, removeField, toMap 
- 
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, put, putIfAbsent, remove, remove, replace, replace, size 
 - 
 
 - 
 
- 
- 
Constructor Detail
- 
BasicDBObject
public BasicDBObject()
Creates an empty object. 
- 
BasicDBObject
public BasicDBObject(int size)
Creates an empty object- Parameters:
 size- an estimate of number of fields that will be inserted
 
- 
BasicDBObject
public BasicDBObject(String key, Object value)
Creates an object with the given key/value- Parameters:
 key- key under which to storevalue- value to store
 
- 
BasicDBObject
public BasicDBObject(Map map)
Creates an object from a map.- Parameters:
 map- map to convert
 
 - 
 
- 
Method Detail
- 
parse
public static BasicDBObject parse(String json)
Parses a string in MongoDB Extended JSON format to aBasicDBObject.- Parameters:
 json- the JSON string- Returns:
 - a corresponding 
BasicDBObjectobject - See Also:
 JsonReader- MongoDB documentation
 - MongoDB Extended JSON
 
 
- 
parse
public static BasicDBObject parse(String json, Decoder<BasicDBObject> decoder)
Parses a string in MongoDB Extended JSON format to aBasicDBObject.- Parameters:
 json- the JSON stringdecoder- the decoder to use to decode the BasicDBObject instance- Returns:
 - a corresponding 
BasicDBObjectobject - See Also:
 JsonReader- MongoDB documentation
 - MongoDB Extended JSON
 
 
- 
append
public BasicDBObject append(String key, Object val)
Add a key/value pair to this object- Overrides:
 appendin classBasicBSONObject- Parameters:
 key- the field nameval- the field value- Returns:
 - this BasicDBObject with the new values added
 
 
- 
isPartialObject
public boolean isPartialObject()
WhethermarkAsPartialObject()was ever called only matters if you are going to upsert and do not want to risk losing fields.- Specified by:
 isPartialObjectin interfaceDBObject- Returns:
 - true if this has been marked as a partial object
 
 
- 
toJson
public String toJson()
Gets a JSON representation of this document using theJsonMode.RELAXEDoutput mode, and otherwise the default settings ofJsonWriterSettings.BuilderandDBObjectCodec.- Returns:
 - a JSON representation of this document
 - Throws:
 CodecConfigurationException- if the document contains types not in the default registry- See Also:
 toJson(JsonWriterSettings),JsonWriterSettings
 
- 
toJson
public String toJson(JsonWriterSettings writerSettings)
Gets a JSON representation of this documentWith the default
DBObjectCodec.- Parameters:
 writerSettings- the json writer settings to use when encoding- Returns:
 - a JSON representation of this document
 - Throws:
 CodecConfigurationException- if the document contains types not in the default registry
 
- 
toJson
public String toJson(Encoder<BasicDBObject> encoder)
Gets a JSON representation of this documentWith the default
JsonWriterSettings.- Parameters:
 encoder- the BasicDBObject codec instance to encode the document with- Returns:
 - a JSON representation of this document
 - Throws:
 CodecConfigurationException- if the registry does not contain a codec for the document values.
 
- 
toJson
public String toJson(JsonWriterSettings writerSettings, Encoder<BasicDBObject> encoder)
Gets a JSON representation of this document- Parameters:
 writerSettings- the json writer settings to use when encodingencoder- the BasicDBObject codec instance to encode the document with- Returns:
 - a JSON representation of this document
 - Throws:
 CodecConfigurationException- if the registry does not contain a codec for the document values.
 
- 
equals
public boolean equals(Object o)
 
- 
hashCode
public int hashCode()
 
- 
toString
public String toString()
Returns a JSON serialization of this object
The output will look like:
{"a":1, "b":["x","y","z"]}- Overrides:
 toStringin classAbstractMap<String,Object>- Returns:
 - JSON serialization
 
 
- 
markAsPartialObject
public void markAsPartialObject()
If this object was retrieved with only some fields (using a field filter) this method will be called to mark it as such.- Specified by:
 markAsPartialObjectin interfaceDBObject
 
- 
copy
public Object copy()
Creates a new instance which is a copy of this BasicDBObject.- Returns:
 - a BasicDBObject with exactly the same values as this instance.
 
 
- 
toBsonDocument
public <TDocument> BsonDocument toBsonDocument(Class<TDocument> documentClass, CodecRegistry codecRegistry)
- Specified by:
 toBsonDocumentin interfaceBson
 
 - 
 
 -