Docs Menu
Docs Home
/ /
Atlas Device SDKs
/ / /

Class MongoCollection

On this page

  • io.realm.mongodb.mongo
  • Method Summary
  • Inherited Methods
  • Method Detail
  • aggregate
  • count
  • deleteMany
  • deleteOne
  • find
  • findOne
  • findOneAndDelete
  • findOneAndReplace
  • findOneAndUpdate
  • getCodecRegistry
  • getDocumentClass
  • getName
  • getNamespace
  • insertMany
  • insertOne
  • updateMany
  • updateOne
  • watch
  • watchAsync
  • watchWithFilter
  • watchWithFilterAsync
  • withCodecRegistry
  • withDocumentClass
io.realm.mongodb.mongo.MongoCollection

The MongoCollection interface provides read and write access to documents.Use MongoDatabase.getCollection to get a collection instance.

Before any access is possible, there must be an active, logged-in user.

Tip

See also:

Modifier and Type
Method and Description

Aggregates documents according to the specified aggregation pipeline specifying an output class.

Aggregates documents according to the specified aggregation pipeline.

Bson filter,
CountOptions options
)

Counts the number of documents in the collection according to the given options.

Bson filter
)

Counts the number of documents in the collection according to the given options.

Counts the number of documents in the collection.

Bson filter
)

Removes all documents from the collection that match the given query filter.

Bson filter
)

Removes at most one document from the collection that matches the given filter.

find <ResultT >(
Bson filter,
FindOptions options
)

Finds all documents in the collection that match the given filter specifying an output class and also using FindOptions to build the query.

find <ResultT >(
Bson filter,
)

Finds all documents in the collection that match the given filter specifying an output class.

Bson filter,
FindOptions options
)

Finds all documents in the collection that match the given filter using FindOptions to build the query.

Bson filter
)

Finds all documents in the collection that match the given filter.

find <ResultT >(
)

Finds all documents in the collection specifying an output class and also using FindOptions to build the query.

find <ResultT >(
)

Finds all documents in the collection specifying an output class.

FindOptions options
)

Finds all documents in the collection using FindOptions to build the query.

find ()

Finds all documents in the collection.

findOne <ResultT >(
Bson filter,
FindOptions options,
)

Finds a document in the collection.

Bson filter,
FindOptions options
)

Finds a document in the collection.

findOne <ResultT >(
Bson filter,
)

Finds a document in the collection.

Bson filter
)

Finds a document in the collection.

findOne <ResultT >(
)

Finds a document in the collection.

Finds a document in the collection.

findOneAndDelete <ResultT >(
Bson filter,
)

Finds a document in the collection and delete it.

Finds a document in the collection and delete it.

findOneAndDelete <ResultT >(
Bson filter,
)

Finds a document in the collection and delete it.

Bson filter
)

Finds a document in the collection and delete it.

findOneAndReplace <ResultT >(
Bson filter,
Bson replacement,
)

Finds a document in the collection and replaces it with the given document.

Bson filter,
Bson replacement,
)

Finds a document in the collection and replaces it with the given document.

findOneAndReplace <ResultT >(
Bson filter,
Bson replacement,
)

Finds a document in the collection and replaces it with the given document.

Bson filter,
Bson replacement
)

Finds a document in the collection and replaces it with the given document.

findOneAndUpdate <ResultT >(
Bson filter,
Bson update,
)

Finds a document in the collection and performs the given update.

Bson filter,
Bson update,
)

Finds a document in the collection and performs the given update.

findOneAndUpdate <ResultT >(
Bson filter,
Bson update,
)

Finds a document in the collection and performs the given update.

Bson filter,
Bson update
)

Finds a document in the collection and performs the given update.

public CodecRegistry

Gets the codec registry for the MongoCollection.

public Class

Gets the class of documents stored in this collection.

public String

Gets the name of this collection

Gets the namespace of this collection, i.e.

Inserts one or more documents.

DocumentT document
)

Inserts the provided document.

Bson filter,
Bson update,
UpdateOptions updateOptions
)

Update all documents in the collection according to the specified arguments.

Bson filter,
Bson update
)

Update all documents in the collection according to the specified arguments.

Bson filter,
Bson update,
UpdateOptions updateOptions
)

Update a single document in the collection according to the specified arguments.

Bson filter,
Bson update
)

Update a single document in the collection according to the specified arguments.

ObjectId[] ids
)

Watches specified IDs in a collection.

BsonValue[] ids
)

Watches specified IDs in a collection.

Watches a collection.

ObjectId[] ids
)

Watches specified IDs in a collection asynchronously.

BsonValue[] ids
)

Watches specified IDs in a collection asynchronously.

Watches a collection asynchronously.

BsonDocument matchFilter
)

Watches a collection.

Document matchFilter
)

Watches a collection.

BsonDocument matchFilter
)

Watches a collection asynchronously.

Document matchFilter
)

Watches a collection asynchronously.

CodecRegistry codecRegistry
)

Creates a new MongoCollection instance with a different codec registry.

Creates a new MongoCollection instance with a different default class to cast any documents returned from the database into.

  • Methods inherited from class java.lang.Object : getClass , hashCode , equals , clone , toString , notify , notifyAll , wait , wait , wait , finalize

Aggregates documents according to the specified aggregation pipeline specifying an output class.All documents will be delivered in the form of an AggregateIterable from which individual elements can be extracted.

Type Parameters

  • ResultT - the target document type of the iterable.

Parameters

  • pipeline - the aggregation pipeline

  • resultClass - the class to decode each document into

Returns

an AggregateIterable from which the results can be extracted

Aggregates documents according to the specified aggregation pipeline.All documents will be delivered in the form of an AggregateIterable from which individual elements can be extracted.

Parameters

  • pipeline - the aggregation pipeline

Returns

an AggregateIterable from which the results can be extracted

Bson filter,
CountOptions options
)

Counts the number of documents in the collection according to the given options.

Parameters

  • filter - the query filter

  • options - the options describing the count

Returns

a task containing the number of documents in the collection

Bson filter
)

Counts the number of documents in the collection according to the given options.

Parameters

  • filter - the query filter

Returns

a task containing the number of documents in the collection

Counts the number of documents in the collection.

Returns

a task containing the number of documents in the collection

Bson filter
)

Removes all documents from the collection that match the given query filter. If no documents match, the collection is not modified.

Parameters

  • filter - the query filter to apply the the delete operation

Returns

a task containing the result of the remove many operation

Bson filter
)

Removes at most one document from the collection that matches the given filter. If no documents match, the collection is not modified.

Parameters

  • filter - the query filter to apply the the delete operation

Returns

a task containing the result of the remove one operation

public FindIterable find <ResultT >(
Bson filter,
FindOptions options
)

Finds all documents in the collection that match the given filter specifying an output class and also using FindOptions to build the query.

All documents will be delivered in the form of a FindIterable from which individual elements can be extracted.

Type Parameters

  • ResultT - the target document type of the iterable.

Parameters

  • filter - the query filter

  • resultClass - the class to decode each document into

  • options - a FindOptions struct

Returns

an iterable containing the result of the find operation

public FindIterable find <ResultT >(
Bson filter,
)

Finds all documents in the collection that match the given filter specifying an output class.All documents will be delivered in the form of a FindIterable from which individual elements can be extracted.

Type Parameters

  • ResultT - the target document type of the iterable.

Parameters

  • filter - the query filter

  • resultClass - the class to decode each document into

Returns

an iterable containing the result of the find operation

Bson filter,
FindOptions options
)

Finds all documents in the collection that match the given filter using FindOptions to build the query.

All documents will be delivered in the form of a FindIterable from which individual elements can be extracted.

Parameters

  • filter - the query filter

  • options - a FindOptions struct

Returns

an iterable containing the result of the find operation

Bson filter
)

Finds all documents in the collection that match the given filter.All documents will be delivered in the form of a FindIterable from which individual elements can be extracted.

Parameters

  • filter - the query filter

Returns

an iterable containing the result of the find operation

public FindIterable find <ResultT >(
)

Finds all documents in the collection specifying an output class and also using FindOptions to build the query.

All documents will be delivered in the form of a FindIterable from which individual elements can be extracted.

Type Parameters

  • ResultT - the target document type of the iterable.

Parameters

  • resultClass - the class to decode each document into

  • options - a FindOptions struct for building the query

Returns

an iterable containing the result of the find operation

public FindIterable find <ResultT >(
)

Finds all documents in the collection specifying an output class.All documents will be delivered in the form of a FindIterable from which individual elements can be extracted.

Type Parameters

  • ResultT - the target document type of the iterable.

Parameters

  • resultClass - the class to decode each document into

Returns

an iterable containing the result of the find operation

FindOptions options
)

Finds all documents in the collection using FindOptions to build the query.

All documents will be delivered in the form of a FindIterable from which individual elements can be extracted.

Parameters

Returns

an iterable containing the result of the find operation

public FindIterable find ()

Finds all documents in the collection.All documents will be delivered in the form of a FindIterable from which individual elements can be extracted.

Returns

an iterable containing the result of the find operation

public RealmResultTask findOne <ResultT >(
Bson filter,
FindOptions options,
)

Finds a document in the collection.

Type Parameters

  • ResultT - the target document type of the iterable.

Parameters

  • filter - the query filter

  • options - a FindOptions struct

  • resultClass - the class to decode each document into

Returns

a task containing the result of the find one operation

Bson filter,
FindOptions options
)

Finds a document in the collection.

Parameters

  • filter - the query filter

  • options - a FindOptions struct

Returns

a task containing the result of the find one operation

public RealmResultTask findOne <ResultT >(
Bson filter,
)

Finds a document in the collection.

Type Parameters

  • ResultT - the target document type of the iterable.

Parameters

  • filter - the query filter

  • resultClass - the class to decode each document into

Returns

a task containing the result of the find one operation

Bson filter
)

Finds a document in the collection.

Parameters

  • filter - the query filter

Returns

a task containing the result of the find one operation

public RealmResultTask findOne <ResultT >(
)

Finds a document in the collection.

Type Parameters

  • ResultT - the target document type

Parameters

  • resultClass - the class to decode each document into

Returns

a task containing the result of the find one operation

Finds a document in the collection.

Returns

a task containing the result of the find one operation

Bson filter,
)

Finds a document in the collection and delete it.

Type Parameters

  • ResultT - the target document type of the iterable.

Parameters

  • filter - the query filter

  • options - a FindOneAndModifyOptions struct

  • resultClass - the class to decode each document into

Returns

a task containing the resulting document

Finds a document in the collection and delete it.

Parameters

Returns

a task containing the resulting document

Bson filter,
)

Finds a document in the collection and delete it.

Type Parameters

  • ResultT - the target document type of the iterable.

Parameters

  • filter - the query filter

  • resultClass - the class to decode each document into

Returns

a task containing the resulting document

Bson filter
)

Finds a document in the collection and delete it.

Parameters

  • filter - the query filter

Returns

a task containing the resulting document

Bson filter,
Bson replacement,
)

Finds a document in the collection and replaces it with the given document.

Type Parameters

  • ResultT - the target document type of the iterable.

Parameters

  • filter - the query filter

  • replacement - the document to replace the matched document with

  • options - a FindOneAndModifyOptions struct

  • resultClass - the class to decode each document into

Returns

a task containing the resulting document

Bson filter,
Bson replacement,
)

Finds a document in the collection and replaces it with the given document.

Parameters

  • filter - the query filter

  • replacement - the document to replace the matched document with

  • options - a FindOneAndModifyOptions struct

Returns

a task containing the resulting document

Bson filter,
Bson replacement,
)

Finds a document in the collection and replaces it with the given document.

Type Parameters

  • ResultT - the target document type of the iterable.

Parameters

  • filter - the query filter

  • replacement - the document to replace the matched document with

  • resultClass - the class to decode each document into

Returns

a task containing the resulting document

Bson filter,
Bson replacement
)

Finds a document in the collection and replaces it with the given document.

Parameters

  • filter - the query filter

  • replacement - the document to replace the matched document with

Returns

a task containing the resulting document

Bson filter,
Bson update,
)

Finds a document in the collection and performs the given update.

Type Parameters

  • ResultT - the target document type of the iterable.

Parameters

  • filter - the query filter

  • update - the update document

  • options - a FindOneAndModifyOptions struct

  • resultClass - the class to decode each document into

Returns

a task containing the resulting document

Bson filter,
Bson update,
)

Finds a document in the collection and performs the given update.

Parameters

Returns

a task containing the resulting document

Bson filter,
Bson update,
)

Finds a document in the collection and performs the given update.

Type Parameters

  • ResultT - the target document type of the iterable.

Parameters

  • filter - the query filter

  • update - the update document

  • resultClass - the class to decode each document into

Returns

a task containing the resulting document

Bson filter,
Bson update
)

Finds a document in the collection and performs the given update.

Parameters

  • filter - the query filter

  • update - the update document

Returns

a task containing the resulting document

public CodecRegistry getCodecRegistry ()

Gets the codec registry for the MongoCollection.

Returns

the CodecRegistry for this collection

Gets the class of documents stored in this collection.If you used the simple MongoDatabase.getCollection(String) to get this collection, this is org.bson.Document .

Returns

the class of documents in this collection

public String getName ()

Gets the name of this collection

Returns

the name

Gets the namespace of this collection, i.e. the database and collection names together.

Returns

the namespace

Inserts one or more documents.

Parameters

  • documents - the documents to insert

Returns

a task containing the result of the insert many operation

DocumentT document
)

Inserts the provided document. If the document is missing an identifier, the client should generate one.

Parameters

  • document - the document to insert

Returns

a task containing the result of the insert one operation

Bson filter,
Bson update,
UpdateOptions updateOptions
)

Update all documents in the collection according to the specified arguments.

Parameters

  • filter - a document describing the query filter, which may not be null.

  • update - a document describing the update, which may not be null. The update to apply must include only update operators.

  • updateOptions - the options to apply to the update operation

Returns

a task containing the result of the update many operation

Bson filter,
Bson update
)

Update all documents in the collection according to the specified arguments.

Parameters

  • filter - a document describing the query filter, which may not be null.

  • update - a document describing the update, which may not be null. The update to apply must include only update operators.

Returns

a task containing the result of the update many operation

Bson filter,
Bson update,
UpdateOptions updateOptions
)

Update a single document in the collection according to the specified arguments.

Parameters

  • filter - a document describing the query filter, which may not be null.

  • update - a document describing the update, which may not be null. The update to apply must include only update operators.

  • updateOptions - the options to apply to the update operation

Returns

a task containing the result of the update one operation

Bson filter,
Bson update
)

Update a single document in the collection according to the specified arguments.

Parameters

  • filter - a document describing the query filter, which may not be null.

  • update - a document describing the update, which may not be null. The update to apply must include only update operators.

Returns

a task containing the result of the update one operation

ObjectId[] ids
)

Watches specified IDs in a collection. This convenience overload supports the use case of non-BsonValue instances of ObjectId by wrapping them in BsonObjectId instances for the user.

Parameters

  • ids - unique object identifiers of the IDs to watch.

Returns

a task that provides access to the stream of change events.

BsonValue[] ids
)

Watches specified IDs in a collection.

Parameters

  • ids - the ids to watch.

Returns

a task that provides access to the stream of change events.

Watches a collection. The resulting stream will be notified of all events on this collection that the active user is authorized to see based on the configured MongoDB Realm rules.

Returns

a task that provides access to the stream of change events.

ObjectId[] ids
)

Watches specified IDs in a collection asynchronously. This convenience overload supports the use case of non-BsonValue instances of ObjectId by wrapping them in BsonObjectId instances for the user.

Parameters

  • ids - unique object identifiers of the IDs to watch.

Returns

a task that provides access to the stream of change events.

BsonValue[] ids
)

Watches specified IDs in a collection asynchronously.

Parameters

  • ids - the ids to watch.

Returns

a task that provides access to the stream of change events.

Watches a collection asynchronously. The resulting stream will be notified of all events on this collection that the active user is authorized to see based on the configured MongoDB Realm rules.

Returns

a task that provides access to the stream of change events.

BsonDocument matchFilter
)

Watches a collection. The provided BSON document will be used as a match expression filter on the change events coming from the stream.See how to define a match filter .

Defining the match expression to filter ChangeEvents is similar to how to define the match expression for triggers

Parameters

  • matchFilter - the $match filter to apply to incoming change events

Returns

a task that provides access to the stream of change events.

Document matchFilter
)

Watches a collection. The provided document will be used as a match expression filter on the change events coming from the stream. This convenience overload supports the use of non-BsonDocument instances for the user.

See how to define a match filter .

Defining the match expression to filter ChangeEvents is similar to how to define the match expression for triggers

Parameters

  • matchFilter - the $match filter to apply to incoming change events

Returns

a task that provides access to the stream of change events.

BsonDocument matchFilter
)

Watches a collection asynchronously. The provided BSON document will be used as a match expression filter on the change events coming from the stream.See how to define a match filter .

Defining the match expression to filter ChangeEvents is similar to how to define the match expression for triggers

Parameters

  • matchFilter - the $match filter to apply to incoming change events

Returns

a task that provides access to the stream of change events.

Document matchFilter
)

Watches a collection asynchronously. The provided document will be used as a match expression filter on the change events coming from the stream. This convenience overload supports the use of non-BsonDocument instances for the user.

See how to define a match filter .

Defining the match expression to filter ChangeEvents is similar to how to define the match expression for triggers

Parameters

  • matchFilter - the $match filter to apply to incoming change events

Returns

a task that provides access to the stream of change events.

CodecRegistry codecRegistry
)

Creates a new MongoCollection instance with a different codec registry.

Parameters

  • codecRegistry - the new CodecRegistry for the collection.

Returns

a new MongoCollection instance with the different codec registry

Creates a new MongoCollection instance with a different default class to cast any documents returned from the database into.

Type Parameters

  • NewDocumentT - The type that the new collection will encode documents from and decode documents to.

Parameters

  • clazz - the default class to which any documents returned from the database will be cast.

Returns

a new MongoCollection instance with the different default class

Back

MongoClient

Next

MongoDatabase