Package com.mongodb

Class BasicDBObject

  • All Implemented Interfaces:
    DBObject, org.bson.BSONObject, org.bson.conversions.Bson


    public class BasicDBObject
    extends org.bson.BasicBSONObject
    implements DBObject, org.bson.conversions.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:
    Serialized Form
    MongoDB documentation
    MongoDB Documents
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.util.AbstractMap

        java.util.AbstractMap.SimpleEntry<K extends java.lang.Object,V extends java.lang.Object>, java.util.AbstractMap.SimpleImmutableEntry<K extends java.lang.Object,V extends java.lang.Object>
    • Constructor Summary

      Constructors 
      Constructor Description
      BasicDBObject​()
      Creates an empty object.
      BasicDBObject​(int size)
      Creates an empty object
      BasicDBObject​(java.lang.String key, java.lang.Object value)
      Creates an object with the given key/value
      BasicDBObject​(java.util.Map map)
      Creates an object from a map.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      BasicDBObject append​(java.lang.String key, java.lang.Object val)
      Add a key/value pair to this object
      java.lang.Object copy​()
      Creates a new instance which is a copy of this BasicDBObject.
      boolean equals​(java.lang.Object o)  
      int hashCode​()  
      boolean isPartialObject​()
      Whether markAsPartialObject() was ever called only matters if you are going to upsert and do not want to risk losing fields.
      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.
      static BasicDBObject parse​(java.lang.String json)
      Parses a string in MongoDB Extended JSON format to a BasicDBObject.
      static BasicDBObject parse​(java.lang.String json, org.bson.codecs.Decoder<BasicDBObject> decoder)
      Parses a string in MongoDB Extended JSON format to a BasicDBObject.
      <TDocument>
      org.bson.BsonDocument
      toBsonDocument​(java.lang.Class<TDocument> documentClass, org.bson.codecs.configuration.CodecRegistry codecRegistry)  
      java.lang.String toJson​()
      Gets a JSON representation of this document using the JsonMode.STRICT output mode, and otherwise the default settings of JsonWriterSettings.Builder and DBObjectCodec.
      java.lang.String toJson​(org.bson.codecs.Encoder<BasicDBObject> encoder)
      Gets a JSON representation of this document
      java.lang.String toJson​(org.bson.json.JsonWriterSettings writerSettings)
      Gets a JSON representation of this document
      java.lang.String toJson​(org.bson.json.JsonWriterSettings writerSettings, org.bson.codecs.Encoder<BasicDBObject> encoder)
      Gets a JSON representation of this document
      java.lang.String toString​()
      Returns a JSON serialization of this object
      • Methods inherited from class org.bson.BasicBSONObject

        containsField, containsKey, get, getBoolean, getBoolean, getDate, getDate, getDouble, getDouble, getInt, getInt, getLong, getLong, getObjectId, getObjectId, getString, getString, putAll, putAll, removeField, toMap
      • Methods inherited from interface org.bson.BSONObject

        containsField, containsKey, get, keySet, put, putAll, putAll, removeField, toMap
      • 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.util.LinkedHashMap

        clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, removeEldestEntry, replaceAll, values
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, containsKey, entry, isEmpty, merge, of, of, of, of, of, of, of, of, of, of, of, ofEntries, put, putIfAbsent, remove, remove, replace, replace, size
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
    • 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​(java.lang.String key,
                             java.lang.Object value)
        Creates an object with the given key/value
        Parameters:
        key - key under which to store
        value - value to store
      • BasicDBObject

        public BasicDBObject​(java.util.Map map)
        Creates an object from a map.
        Parameters:
        map - map to convert
    • Method Detail

      • parse

        public static BasicDBObject parse​(java.lang.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:
        JsonReader
        MongoDB documentation
        MongoDB Extended JSON
      • parse

        public static BasicDBObject parse​(java.lang.String json,
                                          org.bson.codecs.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:
        JsonReader
        MongoDB documentation
        MongoDB Extended JSON
      • append

        public BasicDBObject append​(java.lang.String key,
                                    java.lang.Object val)
        Add a key/value pair to this object
        Overrides:
        append in class org.bson.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 java.lang.String toJson​()
        Gets a JSON representation of this document using the JsonMode.STRICT output mode, and otherwise the default settings of JsonWriterSettings.Builder and DBObjectCodec.
        Returns:
        a JSON representation of this document
        Throws:
        org.bson.codecs.configuration.CodecConfigurationException - if the document contains types not in the default registry
        See Also:
        toJson(JsonWriterSettings), JsonWriterSettings
      • toJson

        public java.lang.String toJson​(org.bson.json.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:
        org.bson.codecs.configuration.CodecConfigurationException - if the document contains types not in the default registry
      • toJson

        public java.lang.String toJson​(org.bson.codecs.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:
        org.bson.codecs.configuration.CodecConfigurationException - if the registry does not contain a codec for the document values.
      • toJson

        public java.lang.String toJson​(org.bson.json.JsonWriterSettings writerSettings,
                                       org.bson.codecs.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:
        org.bson.codecs.configuration.CodecConfigurationException - if the registry does not contain a codec for the document values.
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class org.bson.BasicBSONObject
      • hashCode

        public int hashCode​()
        Overrides:
        hashCode in class org.bson.BasicBSONObject
      • toString

        public java.lang.String toString​()

        Returns a JSON serialization of this object

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

        Overrides:
        toString in class java.util.AbstractMap<java.lang.String,java.lang.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 java.lang.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> org.bson.BsonDocument toBsonDocument​(java.lang.Class<TDocument> documentClass,
                                                                org.bson.codecs.configuration.CodecRegistry codecRegistry)
        Specified by:
        toBsonDocument in interface org.bson.conversions.Bson