com.mongodb
Class BasicDBObject

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap<K,V>
          extended by java.util.LinkedHashMap<String,Object>
              extended by com.mongodb.BasicDBObject
All Implemented Interfaces:
DBObject, Serializable, Cloneable, Map<String,Object>

public class BasicDBObject
extends LinkedHashMap<String,Object>
implements DBObject

A simple implementation of DBObject. A DBObject can be created as follows, using this class:

 DBObject obj = new BasicDBObject();
 obj.put( "foo", "bar" );
 

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
 
Constructor Summary
BasicDBObject()
          Creates an empty object.
BasicDBObject(Map m)
          Creates a DBObject from a map.
BasicDBObject(String key, Object value)
          Convenience CTOR
 
Method Summary
 BasicDBObject append(String key, Object val)
          Add a key/value pair to this object
 boolean containsField(String field)
          Checks if this object contains a given field
 boolean containsKey(String key)
          Deprecated.  
 boolean equals(Object o)
           
 Object get(String key)
          Gets a value from this object
 int getInt(String key)
          Returns the value of a field as an int.
 int getInt(String key, int def)
          Returns the value of a field as an int.
 long getLong(String key)
          Returns the value of a field as a long.
 String getString(String key)
          Returns the value of a field as a string
 boolean isPartialObject()
          Checks if this object is ready to be saved.
 void markAsPartialObject()
          Sets that this object is incomplete and should not be saved.
 Object put(String key, Object val)
          Add a key/value pair to this object
 void putAll(DBObject o)
           
 void putAll(Map m)
           
 Object removeField(String key)
          Deletes a field from this object.
 Map toMap()
          Converts a DBObject to a map.
 String toString()
          Returns a JSON serialization of this object
 
Methods inherited from class java.util.LinkedHashMap
clear, containsValue, get, removeEldestEntry
 
Methods inherited from class java.util.HashMap
clone, containsKey, entrySet, isEmpty, keySet, remove, size, values
 
Methods inherited from class java.util.AbstractMap
hashCode
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.mongodb.DBObject
keySet
 
Methods inherited from interface java.util.Map
containsKey, entrySet, hashCode, isEmpty, keySet, remove, size, values
 

Constructor Detail

BasicDBObject

public BasicDBObject()
Creates an empty object.


BasicDBObject

public BasicDBObject(String key,
                     Object value)
Convenience CTOR

Parameters:
key - key under which to store
value - value to stor

BasicDBObject

public BasicDBObject(Map m)
Creates a DBObject from a map.

Parameters:
m - map to convert
Method Detail

toMap

public Map toMap()
Converts a DBObject to a map.

Specified by:
toMap in interface DBObject
Returns:
the DBObject

removeField

public Object removeField(String key)
Deletes a field from this object.

Specified by:
removeField in interface DBObject
Parameters:
key - the field name to remove
Returns:
the object removed

isPartialObject

public boolean isPartialObject()
Checks if this object is ready to be saved.

Specified by:
isPartialObject in interface DBObject
Returns:
if the object is incomplete

containsField

public boolean containsField(String field)
Checks if this object contains a given field

Specified by:
containsField in interface DBObject
Parameters:
field - field name
Returns:
if the field exists

containsKey

public boolean containsKey(String key)
Deprecated. 

Specified by:
containsKey in interface DBObject

get

public Object get(String key)
Gets a value from this object

Specified by:
get in interface DBObject
Parameters:
key - field name
Returns:
the value

getInt

public int getInt(String key)
Returns the value of a field as an int.

Parameters:
key - the field to look for
Returns:
the field value (or default)

getInt

public int getInt(String key,
                  int def)
Returns the value of a field as an int.

Parameters:
key - the field to look for
def - the default to return
Returns:
the field value (or default)

getLong

public long getLong(String key)
Returns the value of a field as a long.

Parameters:
key - the field to return
Returns:
the field value

getString

public String getString(String key)
Returns the value of a field as a string

Parameters:
key - the field to look up
Returns:
the value of the field, converted to a string

put

public Object put(String key,
                  Object val)
Add a key/value pair to this object

Specified by:
put in interface DBObject
Specified by:
put in interface Map<String,Object>
Overrides:
put in class HashMap<String,Object>
Parameters:
key - the field name
val - the field value
Returns:
the val parameter

putAll

public void putAll(Map m)
Specified by:
putAll in interface DBObject
Specified by:
putAll in interface Map<String,Object>
Overrides:
putAll in class HashMap<String,Object>

putAll

public void putAll(DBObject o)
Specified by:
putAll in interface DBObject

append

public BasicDBObject append(String key,
                            Object val)
Add a key/value pair to this object

Parameters:
key - the field name
val - the field value
Returns:
the val parameter

toString

public String toString()
Returns a JSON serialization of this object

Overrides:
toString in class AbstractMap<String,Object>
Returns:
JSON serialization

markAsPartialObject

public void markAsPartialObject()
Sets that this object is incomplete and should not be saved.

Specified by:
markAsPartialObject in interface DBObject

equals

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