[SERVER-23737] document validation, $exists:1 works, $exists:0 not so much Created: 14/Apr/16  Updated: 22/Apr/16  Resolved: 15/Apr/16

Status: Closed
Project: Core Server
Component/s: Internal Code
Affects Version/s: 3.2.4
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Daniel Farrell Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:


Run code above

Participants:

 Description   

3.2.4 on Centos 7, mongo

---- this first code works as expected -------------------

db.my_coll.drop()
 
vc = { "$and" : 
   [ 
   { "k1" : { "$exists" : "true" } }
   ] } 
      
db.createCollection( "my_coll",
   validator = vc, validationAction = "error" )
 
db.my_coll.insert( { "k1" : 17 } )      #  works
db.my_coll.insert( { "k2" : 17 } )      # fails

---- this code does not work, allows insert -------------

db.my_coll.drop()
 
vc = { "$and" : 
   [ 
   { "k1" : { "$exists" : "false" } }
   ] } 
      
db.createCollection( "my_coll",
   validator = vc, validationAction = "error" )
 
db.my_coll.insert( { "k1" : 17 } )      #  works
db.my_coll.insert( { "k2" : 17 } )      #  works



 Comments   
Comment by Daniel Farrell [ 22/Apr/16 ]

Thanks Dan !

Please advise a newbie ..

Should I create a new ticket- Why did my original request for a document validator pass, return Ok ?

Comment by Daniel Pasette (Inactive) [ 15/Apr/16 ]

There's are a couple issues with your test code. The main thing though is that your call to createCollection is not valid. I don't understand how your first sample would have worked.

I've pasted a version that fails to insert the document containing the field k1, as I believe is the intent.

db.my_coll.drop()
 
vc = { "$and" : 
   [ 
   { "k1" : { "$exists" : false} }
   ] } 
      
db.createCollection( "my_coll",   {validator : vc, validationAction : "error" })
 
db.my_coll.insert( { "k1" : 17 } )   // fails
db.my_coll.insert( { "k2" : 17 } )   // works

To check the validator on your collection run db.getCollectionInfos(). In this case you'll see the following:

> db.getCollectionInfos()
[
	{
		"name" : "my_coll",
		"options" : {
			"validator" : {
				"$and" : [
					{
						"k1" : {
							"$exists" : false
						}
					}
				]
			},
			"validationAction" : "error"
		}
	},
]

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