Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve MongoDb collection validator rules?

Tags:

mongodb

On MongoDB 3.4.4 I've created a collection with a validator, but now some inserts fail this rules and I can't understand why.

  1. Is there a way to output the rules of the validor? I'm afraid the rules applied are different from what I think they are...
  2. Is there a way to improve the error message? "Document failed validation" in this scenario is quite useless.

Thank you!

like image 476
Riccardo Messineo Avatar asked Jul 24 '17 16:07

Riccardo Messineo


People also ask

Does MongoDB support schema validation?

MongoDB uses a flexible schema model, which means that documents in a collection do not need to have the same fields or data types by default. Once you've established an application schema, you can use schema validation to ensure there are no unintended schema changes or improper data types.

What is the command to view all the collections in the MongoDB?

To obtain a list of MongoDB collections, we need to use the Mongo shell command show collections . This command will return all collections created within a MongoDB database.


1 Answers

  1. You can see the validation rules (among other collection information) with db.getCollectionInfos() for all collections or db.getCollectionInfos({name: "myCollection"}) for a specific collection: MongoDB docs

  2. Have a look at this answer.

like image 158
Michal Svorc Avatar answered Sep 18 '22 12:09

Michal Svorc