Help / WLanguage / Managing databases / Big Data / Managing Mongo files
  • Connecting to a MongoDB database
MongoCreate (Example)
Connecting to a MongoDB database
This example shows how to connect to a database. The database is created if it does not exist.
gclConnection is mongoConnection
gclDataBase is mongoDatabase
// 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, ...
// Retrieve the object for accessing the database
gclDataBase = gclConnection.Database[DATABASE_WINDEV_EXAMPLE]
// Search the collection of examples
gclCollection = gclDataBase.Collection[COLLECTION_WINDEV_EXAMPLE]
// Create the collection of examples if it does not exist
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

Last update: 05/26/2022

Send a report | Local help