|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.mongodb.DBCollection
public abstract class DBCollection
This class provides a skeleton implementation of a database collection.
A typical invocation sequence is thus
Mongo mongo = new Mongo( new DBAddress( "localhost", 127017 ) );
DB db = mongo.getDB( "mydb" );
DBCollection collection = db.getCollection( "test" );
| Field Summary | |
|---|---|
protected java.lang.String |
_fullName
|
protected java.util.List<DBObject> |
_hintFields
|
protected java.lang.String |
_name
|
protected java.lang.Class |
_objectClass
|
| Constructor Summary | |
|---|---|
protected |
DBCollection(DB base,
java.lang.String name)
Initializes a new collection. |
| Method Summary | |
|---|---|
java.lang.Object |
apply(DBObject o)
Adds the "private" fields _save, _update, and _id to an object. |
java.lang.Object |
apply(DBObject jo,
boolean ensureID)
Adds the "private" fields _save, _update, and (optionally) _id to an object. |
void |
checkForIDIndex(DBObject key)
Ensures an index on the id field, if one does not already exist. |
protected boolean |
checkReadOnly(boolean strict)
Returns if this collection's database is read-only |
void |
createIndex(DBObject keys)
Forces creation of an index on a set of fields, if one does not already exist. |
java.util.List |
distinct(java.lang.String key)
find distinct values for a key |
java.util.List |
distinct(java.lang.String key,
DBObject query)
find distinct values for a key |
protected abstract void |
doapply(DBObject o)
Adds any necessary fields to a given object before saving it to the collection. |
void |
drop()
Drops (deletes) this collection |
void |
dropIndex(DBObject keys)
|
void |
dropIndex(java.lang.String name)
|
void |
dropIndexes()
Drops all indices from this collection |
void |
dropIndexes(java.lang.String name)
|
void |
ensureIDIndex()
Creates an index on the id field, if one does not already exist. |
void |
ensureIndex(DBObject keys)
Creates an index on a set of fields, if one does not already exist. |
void |
ensureIndex(DBObject keys,
boolean force)
Creates an index on a set of fields, if one does not already exist. |
void |
ensureIndex(DBObject keys,
boolean force,
boolean unique)
|
abstract void |
ensureIndex(DBObject keys,
java.lang.String name)
Ensures an index on this collection (that is, the index will be created if it does not exist). |
abstract void |
ensureIndex(DBObject keys,
java.lang.String name,
boolean unique)
Ensures an optionally unique index on this collection. |
boolean |
equals(java.lang.Object o)
Checks if this collection is equal to another object. |
DBCursor |
find()
Queries for all objects in this collection. |
DBCursor |
find(DBObject ref)
Queries for an object in this collection. |
DBCursor |
find(DBObject ref,
DBObject keys)
Queries for an object in this collection. |
abstract java.util.Iterator<DBObject> |
find(DBObject ref,
DBObject fields,
int numToSkip,
int batchSize)
Finds an object. |
DBObject |
findOne()
Returns a single object from this collection. |
DBObject |
findOne(DBObject o)
Returns a single object from this collection matching the query. |
DBObject |
findOne(DBObject o,
DBObject fields)
Returns a single object from this collection matching the query. |
DBObject |
findOne(java.lang.Object obj)
Finds an object by its id. |
DBObject |
findOne(java.lang.Object obj,
DBObject fields)
Finds an object by its id. |
static java.lang.String |
genIndexName(DBObject keys)
Generate an index name from the set of fields it is over. |
DBCollection |
getCollection(java.lang.String n)
Find a collection that is prefixed with this collection's name. |
long |
getCount()
Returns the number of documents in the collection |
long |
getCount(DBObject query)
Returns the number of documents in the collection that match the specified query |
long |
getCount(DBObject query,
DBObject fields)
Returns the number of documents in the collection that match the specified query |
DB |
getDB()
Returns the database this collection is a member of. |
java.lang.String |
getFullName()
Returns the full name of this collection, with the database name as a prefix. |
java.util.List<DBObject> |
getIndexInfo()
Return a list of the indexes for this collection. |
protected java.lang.Class |
getInternalClass(java.lang.String path)
|
java.lang.String |
getName()
Returns the name of this collection. |
java.lang.Class |
getObjectClass()
Gets the default class for objects in the collection |
DB.WriteConcern |
getWriteConcern()
|
DBObject |
group(DBObject key,
DBObject cond,
DBObject initial,
java.lang.String reduce)
|
int |
hashCode()
Calculates the hash code for this collection. |
abstract void |
insert(DBObject doc)
Saves an document to the database. |
abstract void |
insert(DBObject[] arr)
Saves an array of documents to the database. |
abstract void |
insert(java.util.List<DBObject> list)
Saves an array of documents to the database. |
MapReduceOutput |
mapReduce(DBObject command)
|
MapReduceOutput |
mapReduce(java.lang.String map,
java.lang.String reduce,
java.lang.String outputCollection,
DBObject query)
performs a map reduce operation |
abstract void |
remove(DBObject o)
Removes objects from the database collection. |
void |
rename(java.lang.String newName)
does a rename of this collection to newName |
void |
resetIndexCache()
Clears all indices that have not yet been applied to this collection. |
void |
save(DBObject jo)
Saves an object to this collection. |
void |
setHintFields(java.util.List<DBObject> lst)
Set hint fields for this collection. |
void |
setInternalClass(java.lang.String path,
java.lang.Class c)
|
void |
setObjectClass(java.lang.Class c)
Set a default class for objects in this collection |
void |
setWriteConcern(DB.WriteConcern concern)
|
java.lang.String |
toString()
Returns name of the collection. |
void |
update(DBObject q,
DBObject o)
|
abstract void |
update(DBObject q,
DBObject o,
boolean upsert,
boolean multi)
Performs an update operation. |
void |
updateMulti(DBObject q,
DBObject o)
|
| Methods inherited from class java.lang.Object |
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
protected final java.lang.String _name
protected final java.lang.String _fullName
protected java.util.List<DBObject> _hintFields
protected java.lang.Class _objectClass
| Constructor Detail |
|---|
protected DBCollection(DB base,
java.lang.String name)
base - database in which to create the collectionname - the name of the collection| Method Detail |
|---|
public abstract void insert(DBObject doc)
throws MongoException
doc - object to save
MongoException
public abstract void insert(DBObject[] arr)
throws MongoException
arr - array of documents to save
MongoException
public abstract void insert(java.util.List<DBObject> list)
throws MongoException
list - list of documents to save
MongoException
public abstract void update(DBObject q,
DBObject o,
boolean upsert,
boolean multi)
throws MongoException
q - search query for old object to updateo - object with which to update qupsert - if the database should create the element if it does not existmulti - if the update should be applied to all objects matching (db version 1.1.3 and above)
See http://www.mongodb.org/display/DOCS/Atomic+Operations
MongoException
public void update(DBObject q,
DBObject o)
throws MongoException
MongoException
public void updateMulti(DBObject q,
DBObject o)
throws MongoException
MongoExceptionprotected abstract void doapply(DBObject o)
o - object to which to add the fields
public abstract void remove(DBObject o)
throws MongoException
o - the object that documents to be removed must match
MongoException
public abstract java.util.Iterator<DBObject> find(DBObject ref,
DBObject fields,
int numToSkip,
int batchSize)
throws MongoException
ref - query used to searchfields - the fields of matching objects to returnnumToSkip - will not return the first numToSkip matchesbatchSize - if positive, is the # of objects per batch sent back from the db. all objects that match will be returned. if batchSize < 0, its a hard limit, and only 1 batch will either batchSize or the # that fit in a batch
MongoException
public abstract void ensureIndex(DBObject keys,
java.lang.String name)
throws MongoException
keys - fields to use for indexname - an identifier for the index
MongoException
public abstract void ensureIndex(DBObject keys,
java.lang.String name,
boolean unique)
throws MongoException
keys - fields to use for indexname - an identifier for the indexunique - if the index should be unique
MongoException
public final DBObject findOne(java.lang.Object obj)
throws MongoException
obj - any valid object
null
MongoException
public final DBObject findOne(java.lang.Object obj,
DBObject fields)
obj - any valid objectfields - fields to return
null
public void checkForIDIndex(DBObject key)
throws MongoException
key - an object with an _id field.
MongoException
public void ensureIDIndex()
throws MongoException
MongoException
public final void ensureIndex(DBObject keys)
throws MongoException
keys - an object with a key set of the fields desired for the index
MongoException
public final void createIndex(DBObject keys)
throws MongoException
keys - an object with a key set of the fields desired for the index
MongoException
public final void ensureIndex(DBObject keys,
boolean force)
throws MongoException
keys - an object with a key set of the fields desired for the indexforce - if index creation should be forced, even if it is unnecessary
MongoException
public final void ensureIndex(DBObject keys,
boolean force,
boolean unique)
throws MongoException
MongoExceptionpublic void resetIndexCache()
public static java.lang.String genIndexName(DBObject keys)
keys - the names of the fields used in this index
public void setHintFields(java.util.List<DBObject> lst)
lst - a list of DBObjects to be used as hintspublic final DBCursor find(DBObject ref)
ref - object for which to search
public final DBCursor find(DBObject ref,
DBObject keys)
An empty DBObject will match every document in the collection. Regardless of fields specified, the _id and _ns fields are always returned.
An example that returns the "x", "_id", and "_ns" fields for every document in the collection that has an "x" field:
BasicDBObject keys = new BasicDBObject();
keys.put("x", 1);
DBCursor cursor = collection.find(new BasicDBObject(), keys);
ref - object for which to searchkeys - fields to return
public final DBCursor find()
public final DBObject findOne()
throws MongoException
null if the collection is empty
MongoException
public final DBObject findOne(DBObject o)
throws MongoException
o - the query object
null if no such object exists
MongoException
public final DBObject findOne(DBObject o,
DBObject fields)
o - the query objectfields - fields to return
null if no such object existspublic final java.lang.Object apply(DBObject o)
o - DBObject to which to add fields
public final java.lang.Object apply(DBObject jo,
boolean ensureID)
jo - object to which to add fieldsensureID - whether to add an _id field or not
o
public final void save(DBObject jo)
throws MongoException
jo - the DBObject to save
will add _id field to jo if needed
MongoException
public void dropIndexes()
throws MongoException
MongoException
public void dropIndexes(java.lang.String name)
throws MongoException
MongoException
public void drop()
throws MongoException
MongoException
public long getCount()
throws MongoException
MongoException
public long getCount(DBObject query)
throws MongoException
query - query to select documents to count
MongoException
public long getCount(DBObject query,
DBObject fields)
throws MongoException
query - query to select documents to countfields - fields to return
MongoException
public void rename(java.lang.String newName)
throws MongoException
newName - new collection name (not a full namespace)
MongoException
public DBObject group(DBObject key,
DBObject cond,
DBObject initial,
java.lang.String reduce)
throws MongoException
key - - { a : true }cond - - optional condition on queryreduce - javascript reduce functioninitial - initial value for first match on a key
MongoExceptionpublic java.util.List distinct(java.lang.String key)
public java.util.List distinct(java.lang.String key,
DBObject query)
query - query to apply on collection
public MapReduceOutput mapReduce(java.lang.String map,
java.lang.String reduce,
java.lang.String outputCollection,
DBObject query)
throws MongoException
outputCollection - optional - leave null if want to use temp collectionquery - optional - leave null if you want all objects
MongoException
public MapReduceOutput mapReduce(DBObject command)
throws MongoException
MongoExceptionpublic java.util.List<DBObject> getIndexInfo()
public void dropIndex(DBObject keys)
throws MongoException
MongoException
public void dropIndex(java.lang.String name)
throws MongoException
MongoExceptionpublic DBCollection getCollection(java.lang.String n)
DBCollection users = mongo.getCollection( "wiki" ).getCollection( "users" );
Which is equilalent to
DBCollection users = mongo.getCollection( "wiki.users" );
n - the name of the collection to find
public java.lang.String getName()
public java.lang.String getFullName()
public DB getDB()
protected boolean checkReadOnly(boolean strict)
strict - if an exception should be thrown if the database is read-only
java.lang.RuntimeException - if the database is read-only and strict is setpublic int hashCode()
hashCode in class java.lang.Objectpublic boolean equals(java.lang.Object o)
equals in class java.lang.Objecto - object with which to compare this collection
public java.lang.String toString()
toString in class java.lang.Objectpublic void setObjectClass(java.lang.Class c)
c - the class
java.lang.IllegalArgumentException - if c is not a DBObjectpublic java.lang.Class getObjectClass()
public void setInternalClass(java.lang.String path,
java.lang.Class c)
protected java.lang.Class getInternalClass(java.lang.String path)
public void setWriteConcern(DB.WriteConcern concern)
public DB.WriteConcern getWriteConcern()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||