|
||||||||||
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 String |
_fullName
|
protected List<DBObject> |
_hintFields
|
protected String |
_name
|
protected Class |
_objectClass
|
Constructor Summary | |
---|---|
protected |
DBCollection(DB base,
String name)
Initializes a new collection. |
Method Summary | |
---|---|
Object |
apply(DBObject o)
Adds the "private" fields _id to an object. |
Object |
apply(DBObject jo,
boolean ensureID)
Adds the "private" fields _id to an object. |
protected boolean |
checkReadOnly(boolean strict)
Returns if this collection's database is read-only |
long |
count()
|
long |
count(DBObject query)
|
void |
createIndex(DBObject keys)
Forces creation of an index on a set of fields, if one does not already exist. |
protected abstract void |
createIndex(DBObject keys,
DBObject options)
|
List |
distinct(String key)
find distinct values for a key |
List |
distinct(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(String name)
|
void |
dropIndexes()
Drops all indices from this collection |
void |
dropIndexes(String name)
|
void |
ensureIndex(DBObject keys)
Creates an index on a set of fields, if one does not already exist. |
void |
ensureIndex(DBObject keys,
DBObject optionsIN)
|
void |
ensureIndex(DBObject keys,
String name)
Ensures an index on this collection (that is, the index will be created if it does not exist). |
void |
ensureIndex(DBObject keys,
String name,
boolean unique)
Ensures an optionally unique index on this collection. |
void |
ensureIndex(String name)
|
boolean |
equals(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. |
DBCursor |
find(DBObject ref,
DBObject fields,
int numToSkip,
int batchSize)
Finds an object. |
DBCursor |
find(DBObject ref,
DBObject fields,
int numToSkip,
int batchSize,
int options)
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(Object obj)
Finds an object by its id. |
DBObject |
findOne(Object obj,
DBObject fields)
Finds an object by its id. |
static String |
genIndexName(DBObject keys)
Generate an index name from the set of fields it is over. |
DBCollection |
getCollection(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. |
String |
getFullName()
Returns the full name of this collection, with the database name as a prefix. |
List<DBObject> |
getIndexInfo()
Return a list of the indexes for this collection. |
protected Class |
getInternalClass(String path)
|
String |
getName()
Returns the name of this collection. |
Class |
getObjectClass()
Gets the default class for objects in the collection |
DB.WriteConcern |
getWriteConcern()
Get the write concern for this collection. |
DBObject |
group(DBObject key,
DBObject cond,
DBObject initial,
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(List<DBObject> list)
Saves an array of documents to the database. |
MapReduceOutput |
mapReduce(DBObject command)
|
MapReduceOutput |
mapReduce(String map,
String reduce,
String outputCollection,
DBObject query)
performs a map reduce operation |
abstract void |
remove(DBObject o)
Removes objects from the database collection. |
DBCollection |
rename(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(List<DBObject> lst)
Set hint fields for this collection. |
void |
setInternalClass(String path,
Class c)
|
void |
setObjectClass(Class c)
Set a default class for objects in this collection |
void |
setWriteConcern(DB.WriteConcern concern)
Set the write concern for this collection. |
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 String _name
protected final String _fullName
protected List<DBObject> _hintFields
protected Class _objectClass
Constructor Detail |
---|
protected DBCollection(DB base, String name)
base
- database in which to create the collectionname
- the name of the collectionMethod 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(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
MongoException
protected abstract void doapply(DBObject o)
o
- object to which to add the fieldspublic abstract void remove(DBObject o) throws MongoException
o
- the object that documents to be removed must match
MongoException
public final DBCursor find(DBObject ref, DBObject fields, int numToSkip, int batchSize, int options) 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 batchoptions
- - see Bytes QUERYOPTION_*
MongoException
public final DBCursor find(DBObject ref, DBObject fields, int numToSkip, int batchSize)
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
protected abstract void createIndex(DBObject keys, DBObject options) throws MongoException
MongoException
public final DBObject findOne(Object obj) throws MongoException
obj
- any valid object
null
MongoException
public final DBObject findOne(Object obj, DBObject fields)
obj
- any valid objectfields
- fields to return
null
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(String name)
public final void ensureIndex(DBObject keys) throws MongoException
keys
- an object with a key set of the fields desired for the index
MongoException
public void ensureIndex(DBObject keys, String name) throws MongoException
keys
- fields to use for indexname
- an identifier for the index
MongoException
public void ensureIndex(DBObject keys, 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 void ensureIndex(DBObject keys, DBObject optionsIN) throws MongoException
MongoException
public void resetIndexCache()
public static String genIndexName(DBObject keys)
keys
- the names of the fields used in this index
public void setHintFields(List<DBObject> lst)
lst
- a list of DBObject
s 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 fields are always returned.
An example that returns the "x" and "_id" 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 exists
public final Object apply(DBObject o)
o
- DBObject
to which to add fields
public final 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(String name) throws MongoException
MongoException
public void drop() throws MongoException
MongoException
public long count() throws MongoException
MongoException
public long count(DBObject query) 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 DBCollection rename(String newName) throws MongoException
newName
- new collection name (not a full namespace)
MongoException
public DBObject group(DBObject key, DBObject cond, DBObject initial, String reduce) throws MongoException
key
- - { a : true }cond
- - optional condition on queryreduce
- javascript reduce functioninitial
- initial value for first match on a key
MongoException
public List distinct(String key)
public List distinct(String key, DBObject query)
query
- query to apply on collectionpublic MapReduceOutput mapReduce(String map, String reduce, 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
MongoException
public List<DBObject> getIndexInfo()
public void dropIndex(DBObject keys) throws MongoException
MongoException
public void dropIndex(String name) throws MongoException
MongoException
public DBCollection getCollection(String n)
Which is equilalent toDBCollection users = mongo.getCollection( "wiki" ).getCollection( "users" );
DBCollection users = mongo.getCollection( "wiki.users" );
n
- the name of the collection to find
public String getName()
public String getFullName()
public DB getDB()
protected boolean checkReadOnly(boolean strict)
strict
- if an exception should be thrown if the database is read-only
RuntimeException
- if the database is read-only and strict
is setpublic int hashCode()
hashCode
in class Object
public boolean equals(Object o)
equals
in class Object
o
- object with which to compare this collection
public String toString()
toString
in class Object
public void setObjectClass(Class c)
c
- the class
IllegalArgumentException
- if c
is not a DBObjectpublic Class getObjectClass()
public void setInternalClass(String path, Class c)
protected Class getInternalClass(String path)
public void setWriteConcern(DB.WriteConcern concern)
DB.WriteConcern
for more information.
concern
- write concern to usepublic DB.WriteConcern getWriteConcern()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |