java.lang.Object | |
↳ | com.mongodb.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" );
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
_fullName | |||||||||||
_hintFields | |||||||||||
_name | |||||||||||
_objectClass |
Protected Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Initializes a new collection.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
adds a default query option
| |||||||||||
calls
apply(com.mongodb.DBObject, boolean) with ensureID=true | |||||||||||
calls
doapply(com.mongodb.DBObject) , optionally adding an automatic _id field | |||||||||||
returns the number of documents that match a query.@return
| |||||||||||
returns the number of documents in this collection.@return
| |||||||||||
Forces creation of an index on a set of fields, if one does not already exist.
| |||||||||||
calls
createIndex(com.mongodb.DBObject, com.mongodb.DBObject) with default index options | |||||||||||
Forces creation of an index on a set of fields, if one does not already exist.
| |||||||||||
find distinct values for a key
| |||||||||||
find distinct values for a key
| |||||||||||
Drops (deletes) this collection.
| |||||||||||
Drops an index from this collection
| |||||||||||
Drops an index from this collection
| |||||||||||
Drops an index from this collection
| |||||||||||
Drops all indices from this collection
| |||||||||||
Ensures an index on this collection (that is, the index will be created if it does not exist).
| |||||||||||
calls
ensureIndex(com.mongodb.DBObject, com.mongodb.DBObject) with default options | |||||||||||
calls
ensureIndex(com.mongodb.DBObject, java.lang.String, boolean) with unique=false | |||||||||||
Creates an ascending index on a field with default options, if one does not already exist.
| |||||||||||
Creates an index on a set of fields, if one does not already exist.
| |||||||||||
Finds objects from the database that match a query.
| |||||||||||
Queries for an object in this collection.
| |||||||||||
Queries for all objects in this collection.
| |||||||||||
Calls
find(com.mongodb.DBObject, com.mongodb.DBObject, int, int) and applies the query options | |||||||||||
Queries for an object in this collection.
| |||||||||||
Finds the first document in the query and updates it.
| |||||||||||
calls
findAndModify(com.mongodb.DBObject, com.mongodb.DBObject, com.mongodb.DBObject, boolean, com.mongodb.DBObject, boolean, boolean)
with fields=null, remove=false, returnNew=false, upsert=false | |||||||||||
calls
findAndModify(com.mongodb.DBObject, com.mongodb.DBObject, com.mongodb.DBObject, boolean, com.mongodb.DBObject, boolean, boolean)
with fields=null, sort=null, remove=false, returnNew=false, upsert=false | |||||||||||
calls
findAndModify(com.mongodb.DBObject, com.mongodb.DBObject, com.mongodb.DBObject, boolean, com.mongodb.DBObject, boolean, boolean)
with fields=null, sort=null, remove=true, returnNew=false, upsert=false | |||||||||||
Finds an object by its id.
| |||||||||||
Returns a single object from this collection matching the query.
| |||||||||||
Finds an object by its id.
| |||||||||||
Returns a single object from this collection matching the query.
| |||||||||||
Returns a single object from this collection matching the query.
| |||||||||||
Returns a single object from this collection.
| |||||||||||
Convenience method to generate an index name from the set of fields it is over.
| |||||||||||
Finds a collection that is prefixed with this collection's name.
| |||||||||||
calls
getCount(com.mongodb.DBObject, com.mongodb.DBObject) with null fields.@return | |||||||||||
Returns the number of documents in the collection
that match the specified query
| |||||||||||
calls
getCount(com.mongodb.DBObject, com.mongodb.DBObject, long, long) with limit=0 and skip=0@return | |||||||||||
calls
getCount(com.mongodb.DBObject, com.mongodb.DBObject) with an empty query and null fields. | |||||||||||
Returns the database this collection is a member of.
| |||||||||||
Get the decoder factory for this collection.
| |||||||||||
Get the encoder factory for this collection.
| |||||||||||
Returns the full name of this collection, with the database name as a prefix.
| |||||||||||
Return a list of the indexes for this collection.
| |||||||||||
Returns the name of this collection.
| |||||||||||
Gets the default class for objects in the collection
| |||||||||||
gets the default query options
| |||||||||||
Gets the read preference
| |||||||||||
gets the collections statistics ("collstats" command)
| |||||||||||
Get the write concern for this collection.
| |||||||||||
This method is deprecated.
prefer the
group(com.mongodb.GroupCommand) which is more standard
Applies a group operation | |||||||||||
Applies a group operation@return
| |||||||||||
calls
group(com.mongodb.DBObject, com.mongodb.DBObject, com.mongodb.DBObject, java.lang.String, java.lang.String) with finalize=null@return | |||||||||||
Applies a group operation@return
| |||||||||||
Saves document(s) to the database.
| |||||||||||
Saves document(s) to the database.
| |||||||||||
Saves document(s) to the database.
| |||||||||||
Saves document(s) to the database.
| |||||||||||
Saves document(s) to the database.
| |||||||||||
Saves document(s) to the database.
| |||||||||||
Inserts a document into the database.
| |||||||||||
returns whether or not this is a capped collection
| |||||||||||
performs a map reduce operation
Specify an outputType to control job execution
* INLINE - Return results inline
* REPLACE - Replace the output collection with the job output
* MERGE - Merge the job output with the existing contents of outputTarget
* REDUCE - Reduce the job output with the existing contents of
outputTarget@return
| |||||||||||
performs a map reduce operation@return
| |||||||||||
performs a map reduce operation
Runs the command in REPLACE output mode (saves to named collection)@return
| |||||||||||
performs a map reduce operation@return
| |||||||||||
Removes objects from the database collection.@return
| |||||||||||
calls
remove(com.mongodb.DBObject, com.mongodb.WriteConcern) with the default WriteConcern@return | |||||||||||
Removes objects from the database collection.@return
| |||||||||||
Calls
rename(java.lang.String, boolean) with dropTarget=false | |||||||||||
renames of this collection to newName
| |||||||||||
Clears all indices that have not yet been applied to this collection.
| |||||||||||
resets the default query options
| |||||||||||
Saves an object to this collection (does insert or update based on the object _id).@return
| |||||||||||
calls
save(com.mongodb.DBObject, com.mongodb.WriteConcern) with default WriteConcern | |||||||||||
Set a customer decoder factory for this collection.
| |||||||||||
Set a customer encoder factory for this collection.
| |||||||||||
Set hint fields for this collection (to optimize queries).
| |||||||||||
sets the internal class
| |||||||||||
Sets a default class for objects in this collection; null resets the class to nothing.
| |||||||||||
sets the default query options
| |||||||||||
Sets the read preference for this collection.
| |||||||||||
Set the write concern for this collection.
| |||||||||||
This method is deprecated.
Replaced with ReadPreference.SECONDARY
| |||||||||||
Performs an update operation.@return
| |||||||||||
Performs an update operation.@return
| |||||||||||
calls
update(com.mongodb.DBObject, com.mongodb.DBObject, boolean, boolean) with upsert=false and multi=false@return | |||||||||||
calls
update(com.mongodb.DBObject, com.mongodb.DBObject, boolean, boolean, com.mongodb.WriteConcern) with default WriteConcern.@return | |||||||||||
calls
update(com.mongodb.DBObject, com.mongodb.DBObject, boolean, boolean) with upsert=false and multi=true@return |
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Returns if this collection's database is read-only
| |||||||||||
Adds any necessary fields to a given object before saving it to the collection.
| |||||||||||
gets the internal class
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
Initializes a new collection. No operation is actually performed on the database.
base | database in which to create the collection |
---|---|
name | the name of the collection |
adds a default query option
calls apply(com.mongodb.DBObject, boolean)
with ensureID=true
o | DBObject to which to add fields |
---|
calls doapply(com.mongodb.DBObject)
, optionally adding an automatic _id field
jo | object to add fields to |
---|---|
ensureID | whether to add an _id field |
o
returns the number of documents that match a query.@return
query | query to match |
---|
MongoException |
---|
returns the number of documents in this collection.@return
MongoException |
---|
Forces creation of an index on a set of fields, if one does not already exist.
encoder | the DBEncoder to use |
---|
MongoException |
---|
calls createIndex(com.mongodb.DBObject, com.mongodb.DBObject)
with default index options
keys | an object with a key set of the fields desired for the index |
---|
MongoException |
---|
Forces creation of an index on a set of fields, if one does not already exist.
MongoException |
---|
find distinct values for a key
query | query to match |
---|
Ensures an index on this collection (that is, the index will be created if it does not exist).
keys | fields to use for index |
---|---|
name | an identifier for the index. If null or empty, the default name will be used. |
unique | if the index should be unique |
MongoException |
---|
calls ensureIndex(com.mongodb.DBObject, com.mongodb.DBObject)
with default options
keys | an object with a key set of the fields desired for the index |
---|
MongoException |
---|
calls ensureIndex(com.mongodb.DBObject, java.lang.String, boolean)
with unique=false
keys | fields to use for index |
---|---|
name | an identifier for the index |
MongoException | |
MongoException |
Creates an ascending index on a field with default options, if one does not already exist.
name | name of field to index on |
---|
Creates an index on a set of fields, if one does not already exist.
keys | an object with a key set of the fields desired for the index |
---|---|
optionsIN | options for the index (name, unique, etc) |
MongoException |
---|
Finds objects from the database that match a query. A DBCursor object is returned, that can be iterated to go through the results.
query | query used to search |
---|---|
fields | the fields of matching objects to return |
numToSkip | number of objects to skip |
batchSize | the batch size. This option has a complex behavior, see batchSize(int) |
MongoException |
Queries for an object in this collection.
ref | object for which to search |
---|
Queries for all objects in this collection.
Calls find(com.mongodb.DBObject, com.mongodb.DBObject, int, int)
and applies the query options
query | query used to search |
---|---|
fields | the fields of matching objects to return |
numToSkip | number of objects to skip |
batchSize | the batch size. This option has a complex behavior, see batchSize(int) |
options | - see Bytes QUERYOPTION_* |
MongoException | |
MongoException |
Queries for an object in this collection.
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 search |
---|---|
keys | fields to return |
Finds the first document in the query and updates it.
query | query to match |
---|---|
fields | fields to be returned |
sort | sort to apply before picking first document |
remove | if true, document found will be removed |
update | update to apply |
returnNew | if true, the updated document is returned, otherwise the old document is returned (or it would be lost forever) |
upsert | do upsert (insert if document not present) |
calls findAndModify(com.mongodb.DBObject, com.mongodb.DBObject, com.mongodb.DBObject, boolean, com.mongodb.DBObject, boolean, boolean)
with fields=null, remove=false, returnNew=false, upsert=false
calls findAndModify(com.mongodb.DBObject, com.mongodb.DBObject, com.mongodb.DBObject, boolean, com.mongodb.DBObject, boolean, boolean)
with fields=null, sort=null, remove=false, returnNew=false, upsert=false
calls findAndModify(com.mongodb.DBObject, com.mongodb.DBObject, com.mongodb.DBObject, boolean, com.mongodb.DBObject, boolean, boolean)
with fields=null, sort=null, remove=true, returnNew=false, upsert=false
Finds an object by its id. This compares the passed in value to the _id field of the document
obj | any valid object |
---|---|
fields | fields to return |
null
Returns a single object from this collection matching the query.
o | the query object |
---|
null
if no such object existsMongoException |
---|
Finds an object by its id. This compares the passed in value to the _id field of the document
obj | any valid object |
---|
null
MongoException |
---|
Returns a single object from this collection matching the query.
o | the query object |
---|---|
fields | fields to return |
null
if no such object existsReturns a single object from this collection matching the query.
o | the query object |
---|---|
fields | fields to return |
null
if no such object existsReturns a single object from this collection.
null
if the collection is emptyMongoException |
---|
Convenience method to generate an index name from the set of fields it is over.
keys | the names of the fields used in this index |
---|
Finds a collection that is prefixed with this collection's name. A typical use of this might be
Which is equivalent toDBCollection users = mongo.getCollection( "wiki" ).getCollection( "users" );
DBCollection users = mongo.getCollection( "wiki.users" );
n | the name of the collection to find |
---|
calls getCount(com.mongodb.DBObject, com.mongodb.DBObject)
with null fields.@return
query | query to match |
---|
MongoException |
---|
Returns the number of documents in the collection that match the specified query
query | query to select documents to count |
---|---|
fields | fields to return |
limit | limit the count to this value |
skip | number of entries to skip |
MongoException |
---|
calls getCount(com.mongodb.DBObject, com.mongodb.DBObject, long, long)
with limit=0 and skip=0@return
query | query to match |
---|---|
fields | fields to return |
MongoException |
---|
calls getCount(com.mongodb.DBObject, com.mongodb.DBObject)
with an empty query and null fields.
MongoException |
---|
Returns the database this collection is a member of.
Get the decoder factory for this collection. A null return value means that the default from MongoOptions is being used.
Get the encoder factory for this collection. A null return value means that the default from MongoOptions is being used.
Returns the full name of this collection, with the database name as a prefix.
Return a list of the indexes for this collection. Each object in the list is the "info document" from MongoDB
Gets the default class for objects in the collection
gets the default query options
This method is deprecated.
prefer the group(com.mongodb.GroupCommand)
which is more standard
Applies a group operation
@return
args | object representing the arguments to the group function |
---|
MongoException | |
MongoException |
Applies a group operation@return
cmd | the group command |
---|
MongoException |
calls group(com.mongodb.DBObject, com.mongodb.DBObject, com.mongodb.DBObject, java.lang.String, java.lang.String)
with finalize=null@return
key | - { a : true } |
---|---|
cond | - optional condition on query |
initial | initial value for first match on a key |
reduce | javascript reduce function |
MongoException | |
MongoException |
Applies a group operation@return
key | - { a : true } |
---|---|
cond | - optional condition on query |
initial | initial value for first match on a key |
reduce | javascript reduce function |
finalize | An optional function that can operate on the result(s) of the reduce function. |
MongoException | |
MongoException |
Saves document(s) to the database. if doc doesn't have an _id, one will be added you can get the _id that was added from doc after the insert@return
arr | array of documents to save |
---|
MongoException | |
MongoException |
Saves document(s) to the database. if doc doesn't have an _id, one will be added you can get the _id that was added from doc after the insert@return
list | list of documents to save |
---|---|
concern | the write concern |
MongoException | |
MongoException |
Saves document(s) to the database. if doc doesn't have an _id, one will be added you can get the _id that was added from doc after the insert@return
arr | array of documents to save |
---|
MongoException | |
MongoException |
Saves document(s) to the database. if doc doesn't have an _id, one will be added you can get the _id that was added from doc after the insert@return
arr | array of documents to save |
---|---|
concern | the write concern |
MongoException | |
MongoException |
Saves document(s) to the database. if doc doesn't have an _id, one will be added you can get the _id that was added from doc after the insert@return
list | list of documents to save |
---|
MongoException | |
MongoException |
Saves document(s) to the database. if doc doesn't have an _id, one will be added you can get the _id that was added from doc after the insert@return
arr | array of documents to save |
---|---|
concern | the write concern |
encoder | the DBEncoder to use |
MongoException | |
MongoException |
Inserts a document into the database. if doc doesn't have an _id, one will be added you can get the _id that was added from doc after the insert@return
concern | the write concern |
---|
MongoException | |
MongoException |
returns whether or not this is a capped collection
performs a map reduce operation Specify an outputType to control job execution * INLINE - Return results inline * REPLACE - Replace the output collection with the job output * MERGE - Merge the job output with the existing contents of outputTarget * REDUCE - Reduce the job output with the existing contents of outputTarget@return
map | map function in javascript code |
---|---|
reduce | reduce function in javascript code |
outputTarget | optional - leave null if want to use temp collection |
outputType | set the type of job output |
query | to match |
MongoException | |
MongoException |
performs a map reduce operation@return
command | object representing the parameters |
---|
MongoException |
---|
performs a map reduce operation Runs the command in REPLACE output mode (saves to named collection)@return
map | map function in javascript code |
---|---|
reduce | reduce function in javascript code |
outputTarget | optional - leave null if want to use temp collection |
query | to match |
MongoException | |
MongoException |
performs a map reduce operation@return
command | object representing the parameters |
---|
MongoException |
---|
Removes objects from the database collection.@return
o | the object that documents to be removed must match |
---|---|
concern | WriteConcern for this operation |
encoder | the DBEncoder to use |
MongoException | |
MongoException |
calls remove(com.mongodb.DBObject, com.mongodb.WriteConcern)
with the default WriteConcern@return
o | the object that documents to be removed must match |
---|
MongoException | |
MongoException |
Removes objects from the database collection.@return
o | the object that documents to be removed must match |
---|---|
concern | WriteConcern for this operation |
MongoException | |
MongoException |
Calls rename(java.lang.String, boolean)
with dropTarget=false
newName | new collection name (not a full namespace) |
---|
MongoException |
---|
renames of this collection to newName
newName | new collection name (not a full namespace) |
---|---|
dropTarget | if a collection with the new name exists, whether or not to drop it |
MongoException |
---|
Clears all indices that have not yet been applied to this collection.
resets the default query options
Saves an object to this collection (does insert or update based on the object _id).@return
jo | the DBObject to save |
---|---|
concern | the write concern |
MongoException |
---|
calls save(com.mongodb.DBObject, com.mongodb.WriteConcern)
with default WriteConcern
jo | the DBObject to save
will add _id field to jo if needed |
---|
Set a customer decoder factory for this collection. Set to null to use the default from MongoOptions.
fact | the factory to set. |
---|
Set a customer encoder factory for this collection. Set to null to use the default from MongoOptions.
fact | the factory to set. |
---|
Set hint fields for this collection (to optimize queries).
lst | a list of DBObject s to be used as hints
|
---|
Sets a default class for objects in this collection; null resets the class to nothing.
c | the class |
---|
IllegalArgumentException | if c is not a DBObject
|
---|
sets the default query options
Sets the read preference for this collection. Will be used as default
for reads from this collection; overrides DB & Connection level settings.
See the * documentation for ReadPreference
for more information.
preference | Read Preference to use |
---|
Set the write concern for this collection. Will be used for
writes to this collection. Overrides any setting of write
concern at the DB level. See the documentation for
WriteConcern
for more information.
concern | write concern to use |
---|
This method is deprecated.
Replaced with ReadPreference.SECONDARY
makes this query ok to run on a slave node
Performs an update operation.@return
q | search query for old object to update |
---|---|
o | object with which to update q |
upsert | if the database should create the element if it does not exist |
multi | if the update should be applied to all objects matching (db version 1.1.3 and above). An object will not be inserted if it does not exist in the collection and upsert=true and multi=true. See http://www.mongodb.org/display/DOCS/Atomic+Operations |
concern | the write concern |
encoder | the DBEncoder to use |
MongoException | |
MongoException |
Performs an update operation.@return
q | search query for old object to update |
---|---|
o | object with which to update q |
upsert | if the database should create the element if it does not exist |
multi | if the update should be applied to all objects matching (db version 1.1.3 and above). An object will not be inserted if it does not exist in the collection and upsert=true and multi=true. See http://www.mongodb.org/display/DOCS/Atomic+Operations |
concern | the write concern |
MongoException | |
MongoException |
calls update(com.mongodb.DBObject, com.mongodb.DBObject, boolean, boolean)
with upsert=false and multi=false@return
q | search query for old object to update |
---|---|
o | object with which to update q |
MongoException | |
MongoException |
calls update(com.mongodb.DBObject, com.mongodb.DBObject, boolean, boolean, com.mongodb.WriteConcern)
with default WriteConcern.@return
q | search query for old object to update |
---|---|
o | object with which to update q |
upsert | if the database should create the element if it does not exist |
multi | 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 | |
MongoException |
calls update(com.mongodb.DBObject, com.mongodb.DBObject, boolean, boolean)
with upsert=false and multi=true@return
q | search query for old object to update |
---|---|
o | object with which to update q |
MongoException | |
MongoException |
Returns if this collection's database is read-only
strict | if an exception should be thrown if the database is read-only |
---|
RuntimeException | if the database is read-only and strict is set
|
---|
Adds any necessary fields to a given object before saving it to the collection.
o | object to which to add the fields |
---|