Platforms to show: All Mac Windows Linux Cross-Platform

Back to MongoCollectionMBS class.

MongoCollectionMBS.Aggregate(flags as Integer, pipelineJSON as String, OptionsJSON as String = "") as MongoCursorMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method MongoDB MBS MongoDB Plugin 23.1 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Creates an aggregate collection.
Example

dim collection as MongoCollectionMBS // your collection
Dim json As String = "["+_
"{ ""$match"" : { ""index"" : 123 } },"+_
"{ ""$sort"" : { ""index"" : -1 } },"+_
"{ ""$limit"" : 2 }"+_
"]"

cursor = Collection.Aggregate(Collection.kFlagNone, json)
// loop over cursor

This function creates a cursor which sends the aggregate command on the underlying collection upon the first call to CursorNext. For more information on building aggregation pipelines, see the MongoDB Manual entry on the aggregate command.
Returns a new cursor on success.

Please review the documentation for MongoDB on how to build the JSON for pipeline or options.

FlagValueDescription
None0Specify no query flags.
TailableCursor2Cursor will not be closed when the last data is retrieved. You can resume this cursor later.
SecondaryOk4Allow query of replica set secondaries.
NoCursorTimeout16The server normally times out an idle cursor after an inactivity period (10 minutes). This prevents that.
AwaitData32Use with TailableCursor. Block rather than returning no data. After a period, time out.
Exhaust64Stream the data down full blast in multiple “reply” packets. Faster when you are pulling down a lot of data and you know you want to retrieve it all. Only applies to cursors created from a find operation (i.e. find)
Partial128Get partial results from mongos if some shards are down (instead of throwing an error).

In case of an error, raises MongoExceptionMBS.

MongoCollectionMBS.Command(commandJSON as String, OptionsJSON as String = "") as String

Type Topic Plugin Version macOS Windows Linux iOS Targets
method MongoDB MBS MongoDB Plugin 23.1 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Execute a command on the server.
Example

dim collection as MongoCollectionMBS // your collection

dim Reply as String = collection.Command("{ ""hello"": 1 }")

Consult the MongoDB Manual entry on Database Commands for each command’s arguments.

In case of an error, raises MongoExceptionMBS.

MongoCollectionMBS.Constructor   Private

Type Topic Plugin Version macOS Windows Linux iOS Targets
method MongoDB MBS MongoDB Plugin 22.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
The private constructor.

MongoCollectionMBS.Copy as MongoCollectionMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method MongoDB MBS MongoDB Plugin 22.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Performs a deep copy of the collection struct and its configuration.

This function does not copy the contents of the collection on the MongoDB server.

MongoCollectionMBS.DeleteMany(selectorJSON as String, OptionsJSON as String = "") as String

Type Topic Plugin Version macOS Windows Linux iOS Targets
method MongoDB MBS MongoDB Plugin 22.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Deletes many records.

This function removes all documents in the given collection that match selector.

To delete at most one matching document, use DeleteOne().
Returns string with JSON about errors on server.

MongoCollectionMBS.DeleteOne(selectorJSON as String, OptionsJSON as String = "") as String

Type Topic Plugin Version macOS Windows Linux iOS Targets
method MongoDB MBS MongoDB Plugin 22.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Deletes one record.
Example

// delete record by object ID

Dim oid As New JSONItem
oid.Value("$oid") = "632752c4031bafedd50fbf21"

Dim Record As New JSONItem
Record.Value("_id") = oid

Dim Result As String = Collection.DeleteOne(Record.toString)
// e.g. { "deletedCount" : 1 }

This function removes at most one document in the given collection that matches selector.

To delete all matching documents, use DeleteMany().

Returns string with JSON about errors on server.

MongoCollectionMBS.EstimatedDocumentCount(OptionsJSON as String = "") as Int64

Type Topic Plugin Version macOS Windows Linux iOS Targets
method MongoDB MBS MongoDB Plugin 22.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Executes a count query on collection.

The count returned is not guaranteed to be accurate.

see also
http://mongoc.org/libmongoc/current/mongoc_collection_estimated_document_count.html

See also:

Some examples using this method:

MongoCollectionMBS.EstimatedDocumentCount(OptionsJSON as String, byref ReplyJSON as String) as Int64

Type Topic Plugin Version macOS Windows Linux iOS Targets
method MongoDB MBS MongoDB Plugin 22.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Executes a count query on collection.

The count returned is not guaranteed to be accurate.

see also
http://mongoc.org/libmongoc/current/mongoc_collection_estimated_document_count.html

See also:

MongoCollectionMBS.Find(FilterJSON as String, OptionsJSON as String = "") as MongoCursorMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method MongoDB MBS MongoDB Plugin 22.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Query on collection, passing arbitrary query options to the server in opts.
Example

const URL = "mongodb://localhost/"

dim uri as new MongoURIMBS(URL)
dim client as new MongoClientMBS(uri)

dim database as MongoDatabaseMBS = client.Database("local")
dim Collection as MongoCollectionMBS = database.Collection("test")

dim Filter as new JSONItem
'filter.Value("Hello") = "World"

dim sortOrder as new JSONItem
sortOrder.Value("hello") = -1 // descending

dim options as new JSONItem
options.Value("limit") = 10
options.Value("sort") = sortOrder

dim cursor as MongoCursorMBS = Collection.Find(filter.toString, options.toString)
dim Record as string

while cursor.NextRecord(record)

MessageBox record

wend

See also find command documented here:
https://www.mongodb.com/docs/manual/reference/command/find/

MongoCollectionMBS.FindIndexes(OptionsJSON as String = "") as MongoCursorMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method MongoDB MBS MongoDB Plugin 22.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Fetches a cursor containing documents, each corresponding to an index on this collection.

MongoCollectionMBS.InsertMany(documentArrayJSON as String, OptionsJSON as String = "") as String

Type Topic Plugin Version macOS Windows Linux iOS Targets
method MongoDB MBS MongoDB Plugin 22.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Insert documents into collection.

Please pass a JSON array as string for all documents. Each entry in the array should be a JSON object with the document data.

For any document that does not have an "_id" field, a bson_oid_t will be generated locally and added to the document. If you must know the inserted document's _id, generate it in your code and include it in the document. The _id you generate can be a bson_oid_t or any other non-array BSON type.

The reply is filled out with an "insertedCount" field. If there is a server error then reply may contain a "writeErrors" array and/or a "writeConcernErrors" array.

See also:

MongoCollectionMBS.InsertMany(documentJSON() as String, OptionsJSON as String = "") as String

Type Topic Plugin Version macOS Windows Linux iOS Targets
method MongoDB MBS MongoDB Plugin 22.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Insert documents into collection.

Please pass an array with a JSON object in each entry as string.

For any document that does not have an "_id" field, a bson_oid_t will be generated locally and added to the document. If you must know the inserted document's _id, generate it in your code and include it in the document. The _id you generate can be a bson_oid_t or any other non-array BSON type.

The reply is filled out with an "insertedCount" field. If there is a server error then reply may contain a "writeErrors" array and/or a "writeConcernErrors" array.

See also:

MongoCollectionMBS.InsertOne(documentJSON as String, OptionsJSON as String = "") as String

Type Topic Plugin Version macOS Windows Linux iOS Targets
method MongoDB MBS MongoDB Plugin 22.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Inserts a document into collection.
Example

const URL = "mongodb://localhost/"

dim uri as new MongoURIMBS(URL)
dim client as new MongoClientMBS(uri)

dim database as MongoDatabaseMBS = client.Database("local")
dim Collection as MongoCollectionMBS = database.Collection("test")

dim j as new JSONItem
j.Value("Hello") = "World"

dim reply as string = Collection.InsertOne(j.ToString)

// reply is { "insertedCount" : 1 }

Break

To insert an array of documents, see MongoDB.InsertMany.

If no _id element is found in document, then an oid will be generated locally and added to the document. If you must know the inserted document’s _id, generate it in your code and include it in the document. The _id you generate can be an old or any other non-array BSON type.

The reply is filled out with an “insertedCount” field. If there is a server error then reply contains either a “writeErrors” array with one subdocument or a “writeConcernErrors” array.

In case of an error, raises MongoExceptionMBS.

OptionsJSON may be empty or a JSON document with additional command options:

writeConcern: the write concern.
sessionId: The session ID.
validate: The validation flags.
bypassDocumentValidation: Set to true to skip server-side schema validation of the provided BSON documents.

Some examples using this method:

MongoCollectionMBS.Keys as String()

Type Topic Plugin Version macOS Windows Linux iOS Targets
method MongoDB MBS MongoDB Plugin 22.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Queries current collection to find all the key names.

You may store different JSON documents in the collection with different keys.
This function scans all documents to get the root keys and returns the list.

MongoCollectionMBS.Rename(NewDatabaseName as String, NewCollectionName as String, DropTargetBeforeRename as Boolean = false, OptionsJSON as String = "") as boolean

Type Topic Plugin Version macOS Windows Linux iOS Targets
method MongoDB MBS MongoDB Plugin 22.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
This function is a helper to rename an existing collection on a MongoDB server.

The name of the collection will also be updated internally so it is safe to continue using this collection after the rename. Additional operations will occur on renamed collection.

NewDatabaseName: The name of the new database.
NewCollectionName: The new name for the collection.
DropTargetBeforeRename: If an existing collection matches the new name, drop it before the rename.

Returns true if successful. Returns false and raises exception in case of an error.

MongoCollectionMBS.ReplaceOne(selectorJSON as String, replacementJSON as String, OptionsJSON as String = "") as String

Type Topic Plugin Version macOS Windows Linux iOS Targets
method MongoDB MBS MongoDB Plugin 22.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
This replaces documents in collection that match selector with replacement.
Example

const URL = "mongodb://localhost/"

dim uri as new MongoURIMBS(URL)
dim client as new MongoClientMBS(uri)

dim database as MongoDatabaseMBS = client.Database("local")
dim Collection as MongoCollectionMBS = database.Collection("test")

// the conditions to match records
dim j as new JSONItem
j.Value("Hello") = "World"

// the new values
dim u as new JSONItem
u.Value("OtherField") = 456
u.Value("Hello") = "World"

dim reply as string = Collection.ReplaceOne(j.ToString, u.ToString)

Break

selectorJSON: A JSON containing the query to match the document for updating.
updateJSON: A JSON containing the replacement document.

The reply JSON is populated with the fields matchedCount, modifiedCount, upsertedCount, and optionally upsertedId if applicable. If there is a server error then reply contains either a writeErrors array with one subdocument or a writeConcernErrors array.

In case of an error, raises MongoExceptionMBS.

MongoCollectionMBS.UpdateMany(selectorJSON as String, updateJSON as String, OptionsJSON as String = "") as String

Type Topic Plugin Version macOS Windows Linux iOS Targets
method MongoDB MBS MongoDB Plugin 22.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Updates all documents in collection that match selector.
Example

const URL = "mongodb://localhost/"

dim uri as new MongoURIMBS(URL)
dim client as new MongoClientMBS(uri)

dim database as MongoDatabaseMBS = client.Database("local")
dim Collection as MongoCollectionMBS = database.Collection("test")

// the conditions to match records
dim j as new JSONItem
j.Value("Hello") = "World"

// the new values
dim u as new JSONItem
u.Value("OtherField") = 123

// and the update, where we define to set those values
dim s as new JSONItem
s.Value("$set") = u

dim reply as string = Collection.UpdateMany(j.ToString, s.ToString)

// reply: { "modifiedCount" : 1, "matchedCount" : 1, "upsertedCount" : 0 }
Break

In case of an error, raises MongoExceptionMBS.

To update at most one document see UpdateOne().

The result JSON is filled out with fields matchedCount, modifiedCount, and optionally upsertedId if applicable. If there is a server error then reply contains either a “writeErrors” array with one subdocument or a “writeConcernErrors” array.

MongoCollectionMBS.UpdateOne(selectorJSON as String, updateJSON as String, OptionsJSON as String = "") as String

Type Topic Plugin Version macOS Windows Linux iOS Targets
method MongoDB MBS MongoDB Plugin 22.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Updates at most one document in collection that matches selector.
Example

const URL = "mongodb://localhost/"

dim uri as new MongoURIMBS(URL)
dim client as new MongoClientMBS(uri)

dim database as MongoDatabaseMBS = client.Database("local")
dim Collection as MongoCollectionMBS = database.Collection("test")

// the conditions to match records
dim j as new JSONItem
j.Value("Hello") = "World"

// the new values
dim u as new JSONItem
u.Value("OtherField") = 123

// and the update, where we define to set those values
dim s as new JSONItem
s.Value("$set") = u

dim reply as string = Collection.UpdateOne(j.ToString, s.ToString)

// reply: { "modifiedCount" : 1, "matchedCount" : 1, "upsertedCount" : 0 }
break

In case of an error, raises MongoExceptionMBS.

To update multiple documents see UpdateMany().

Return is a JSON filled out with fields matchedCount, modifiedCount, and optionally upsertedId if applicable. If there is a server error then reply contains either a “writeErrors” array with one subdocument or a “writeConcernErrors” array.

for Options you may e.g. pass {"upsert":true} to create a new record if the old one doesn't exist.

MongoCollectionMBS.Watch(pipelineJSON as String, OptionsJSON as String = "") as MongoChangeStreamMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method MongoDB MBS MongoDB Plugin 23.0 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Creates a change stream for a collection.

It is preferred to call this function over using a raw aggregation to create a change stream.

This function uses the read preference and read concern of the collection. If the change stream needs to re-establish connection, the same read preference will be used. This may happen if the change stream encounters a resumable error.

Warning A change stream is only supported with majority read concern.
This function is considered a retryable read operation. Upon a transient error (a network error, errors due to replica set failover, etc.) the operation is safely retried once. If retryreads is false in the URI the retry behavior does not apply.

Call this method on the collection which the change stream listens to.

pipelineJSON: A JSON representing an aggregation pipeline appended to the change stream. This may be an empty document.

OptionsJSON: A JSON containing change stream options. options may be "" or a JSON document with additional command options:

batchSize: An integer representing number of documents requested to be returned on each call to NextChange()

resumeAfter: A Document representing the logical starting point of the change stream. The result of StreamResumeToken() or the _id field of any change received from a change stream can be used here. This option is mutually exclusive with startAfter and startAtOperationTime.

startAfter: A Document representing the logical starting point of the change stream. Unlike resumeAfter, this can resume notifications after an “invalidate” event. The result of StreamResumeToken() or the _id field of any change received from a change stream can be used here. This option is mutually exclusive with resumeAfter and startAtOperationTime.

startAtOperationTime: A Timestamp. The change stream only provides changes that occurred at or after the specified timestamp. Any command run against the server will return an operation time that can be used here. This option is mutually exclusive with resumeAfter and startAfter.

maxAwaitTimeMS: An int64 representing the maximum amount of time a call to StreamResumeToken() will block waiting for data

fullDocument: An optional UTF-8 string. Set this option to “default”, “updateLookup”, “whenAvailable”, or “required”, If unset, The string “default” is assumed. Set this option to “updateLookup” to direct the change stream cursor to lookup the most current majority-committed version of the document associated to an update change stream event.

fullDocumentBeforeChange: An optional UTF-8 string. Set this option to “whenAvailable”, “required”, or “off”. When unset, the default value is “off”. Similar to “fullDocument”, but returns the value of the document before the associated change.

comment: A JSON specifying the comment to attach to this command. The comment will appear in log messages, profiler output, and currentOp output. Only string values are supported prior to MongoDB 4.4.

The items on this page are in the following plugins: MBS MongoDB Plugin.


The biggest plugin in space...