Home | Sign in | English UK
  • Creating a collection
MongoCreateCollection (Example)
Creating a collection
This example is used to connect to a database. The database is created if it does not exist.
// Connect to the MongoDB server on the example database (creates the database if it does not exist)
gclConnection = MongoCreate(StringBuild("mongodb://%1:%2/%3", EDT_ServerAddress, ...
// Retrieves the object for accessing the database
gclDatabase = gclConnection.Database[DATABASE_WINDEV_EXAMPLE]
// Finds the example collection
gclCollection = gclDatabase.Collection[COLLECTION_WINDEV_EXAMPLE]
// If the example collection does not exist, create it
IF gclCollection = Null THEN
cOption is mongoCollectionOption
// Options used to limit the collection size
// cOption..MaxSize: If the collection reaches or exceeds this size (in bytes),
// the oldest elements will be deleted
// cOption..MaxNbDocument: If the collection reaches or exceeds this size
// in number of documents,
// the oldest elements will be deleted
// Validate the added documents
// cOption..Validator: JSON document describing the format of added documents
// This example of validator checks whether the document contains
// at least the Phone item or the Email item
cOption..Validator = "{ ""$or"": [{""Phone"": {""$type"": ""string"" } }," + ...
"{ ""Email"": {""$type"": ""string"" } } ] }"
// cOption..ValidationLevel: Defines how the validator is used.
// The possible values are:
// "off": does not use the validator
// "strict": uses the validator for the additions and the modifications
// "moderate": uses the validator for the additions,
// uses the validator in modification only if the modified document is valid
cOption..ValidationLevel = "strict"
MongoCreateCollection(gclBase, COLLECTION_WINDEV_EXAMPLE, cOption)
gclCollection = gclDatabase.Collection[COLLECTION_WINDEV_EXAMPLE]
Minimum version required
  • Version 22
This page is also available for…
Click [Add] to post a comment