[DRIVERS-269] Create/modify collection helpers needs to support creating "validators" Created: 07/Oct/15  Updated: 15/May/19  Resolved: 22/Jan/16

Status: Closed
Project: Drivers
Component/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Rathi Gnanasekaran Assignee: Barrie Segal
Resolution: Done Votes: 0
Labels: newdriver
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on RUBY-1055 support creating a collection with op... Closed
depends on CXX-720 Add document validation support to Cr... Closed
depends on JAVA-2008 Add document validation support to Cr... Closed
depends on PHPLIB-135 Create/modify collection helpers need... Closed
depends on RUST-8 Create/modify collection helpers need... Closed
depends on CSHARP-1439 Add document validation support to Cr... Closed
depends on CXX-719 Add document validation support to Cr... Closed
Related
related to CXX-725 Add a collMod helper that takes docum... Closed
is related to DRIVERS-250 Support bypassDocumentValidation Closed
Driver Compliance:
Key Status/Resolution FixVersion
JAVA-2008 Done 3.2.0
CSHARP-1439 Done 1.11, 2.2
RUBY-1055 Done 2.2.0
SCALA-219 Done 1.1.0
CXX-719 Done
CXX-720 Done 3.0.0-rc0
PHPLIB-135 Done 1.0.0-beta2
RUST-8 Fixed 1.1.0
SWIFT-460 Done

 Description   

Examples (3.1.9-pre 2015-10-06-nightly)

Setting up a validator for a collection

"Creating a validator during collection creation"

> db.runCommand(
{
	"create": "collectionName",
	"validator": { /* $query document. (Almost) all $query operators allowed*/
		"fieldName": {
			"$gte": 1024
		}
	},
	/* (default: "" == "strict") */
	"validationLevel": "", /* "" | "strict" | "off" | "moderate" */
	/*  (default: "" == "error") */
	"validationAction": "" /* "" | "error" | "warn" */
})

"The above example will output:"

{ "ok" : 1 }

Note: unrecognized options are not preserved.

"Creating a validator on existing collection"

> db.existingCollectionName.insert({"my": "document"})
WriteResult({ "nInserted" : 1 })
> db.runCommand(
{
	"collMod": "existingCollectionName",
	"validator": { /* Same $query document as for the 'create' command */
		"fieldName": {
			"$gte": 1024
		}
	},
	/* (default: "" == "strict") */
	"validationLevel": "", /* "" | "strict" | "off" | "moderate" */
	/*  (default: "" == "error") */
	"validationAction": "" /* "" | "error" | "warn" */
})

"The above example will output:"

{ "ok" : 1 }



"Verifying you validators were created/showing existing validators on a collection"

"Showing validators for a collection"

> db.runCommand(
{
	"listCollections": 1,
	"filter": {
		"name": "collectionName"
	}
})

"The above example will output:"

{
	"cursor" : {
		"id" : NumberLong(0),
		"ns" : "test.$cmd.listCollections",
		"firstBatch" : [
			{
				"name" : "collectionName",
				"options" : {
					"validator" : {
						"fieldName" : {
							"$gte" : 1024
						}
					}
				}
			}
		]
	},
	"ok" : 1
}



"Errors"

"Invalid validationLevel or validationAction"

{ "ok" : 0, "errmsg" : "invalid validation level: brokenLevel", "code" : 2 }
{ "ok" : 0, "errmsg" : "invalid validation action: brokenAction", "code" : 2 }

"Using illegal operators"

{
	"ok" : 0,
	"errmsg" : "$text is not allowed in collection validators",
	"code" : 72
}
{ "ok" : 0, "errmsg" : "unknown operator: $texts", "code" : 2 }



 Comments   
Comment by Mark Benvenuto [ 28/Oct/15 ]

C++ Legacy Driver supports createCollectionWithOptions which takes an a BSON document for collection options called extraOptions which can support a validator.

Comment by Jeremy Mikola [ 23/Oct/15 ]

PHP (legacy driver) doesn't need a ticket, as createCollection() takes arbitrary options. We also don't have a "collMod" helper there.

PHPLIB does need a ticket, as we have a CreateCollection helper that only takes specific options; however, there is no "collMod" helper yet. I'll create the ticket and link it up.

Comment by A. Jesse Jiryu Davis [ 23/Oct/15 ]

Validated for C: mongoc_database_create_collection accepts arbitrary "BSON options" and doesn't prohibit "validator" or do anything special with it.

Comment by Bernie Hackett [ 07/Oct/15 ]

PyMongo is future proof against any additions to creating a collection already.

Comment by David Golden [ 07/Oct/15 ]

Perl driver has no collection create/modify helpers.

Generated at Thu Feb 08 08:21:08 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.