Package com.mongodb

Class 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. A DBObject can be created as follows, using this class:
 DBObject obj = new BasicDBObject();
 obj.put( "foo", "bar" );
 
See Also:
MongoDB documentation
MongoDB Documents
  • Constructor Details

    • 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 store
      value - value to store
    • BasicDBObject

      public BasicDBObject(Map map)
      Creates an object from a map.
      Parameters:
      map - map to convert
  • Method Details

    • parse

      public static BasicDBObject parse(String json)
      Parses a string in MongoDB Extended JSON format to a BasicDBObject.
      Parameters:
      json - the JSON string
      Returns:
      a corresponding BasicDBObject object
      See Also:
      MongoDB documentation
      MongoDB Extended JSON
    • parse

      public static BasicDBObject parse(String json, Decoder<BasicDBObject> decoder)
      Parses a string in MongoDB Extended JSON format to a BasicDBObject.
      Parameters:
      json - the JSON string
      decoder - the decoder to use to decode the BasicDBObject instance
      Returns:
      a corresponding BasicDBObject object
      See Also:
      MongoDB documentation
      MongoDB Extended JSON
    • append

      public BasicDBObject append(String key, Object val)
      Add a key/value pair to this object
      Overrides:
      append in class BasicBSONObject
      Parameters:
      key - the field name
      val - the field value
      Returns:
      this BasicDBObject with the new values added
    • isPartialObject

      public boolean isPartialObject()
      Whether markAsPartialObject() was ever called only matters if you are going to upsert and do not want to risk losing fields.
      Specified by:
      isPartialObject in interface DBObject
      Returns:
      true if this has been marked as a partial object
    • toJson

      public String toJson()
      Gets a JSON representation of this document using the JsonMode.RELAXED output mode, and otherwise the default settings of JsonWriterSettings.Builder and DBObjectCodec.
      Returns:
      a JSON representation of this document
      Throws:
      CodecConfigurationException - if the document contains types not in the default registry
      See Also:
    • toJson

      public String toJson(JsonWriterSettings writerSettings)
      Gets a JSON representation of this document

      With 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 document

      With 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 encoding
      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.
    • equals

      public boolean equals(Object o)
      Specified by:
      equals in interface Map<String,Object>
      Overrides:
      equals in class BasicBSONObject
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Map<String,Object>
      Overrides:
      hashCode in class BasicBSONObject
    • toString

      public String toString()

      Returns a JSON serialization of this object

      The output will look like: {"a":1, "b":["x","y","z"]}

      Overrides:
      toString in class AbstractMap<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:
      markAsPartialObject in interface DBObject
    • 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:
      toBsonDocument in interface Bson