Skip to content

Instantly share code, notes, and snippets.

@jlescalonap
Created February 29, 2024 16:30
Show Gist options
  • Save jlescalonap/2e78c2342340b049748dfd027d2b0720 to your computer and use it in GitHub Desktop.
Save jlescalonap/2e78c2342340b049748dfd027d2b0720 to your computer and use it in GitHub Desktop.
Ejemplo de validación de schema en MongoDb.
db.createCollection('posts', {
validator: {
$jsonSchema: {
bsonType: 'object',
required: ['title', 'text', 'creator', 'comments'],
properties: {
title: {
bsonType: 'string',
description: 'must be a string and is required'
},
text: {
bsonType: 'string',
description: 'must be a string and is required'
},
creator: {
bsonType: 'objectId',
description: 'must be an objectid and is required'
},
comments: {
bsonType: 'array',
description: 'must be an array and is required',
items: {
bsonType: 'object',
required: ['text', 'author'],
properties: {
text: {
bsonType: 'string',
description: 'must be a string and is required'
},
author: {
bsonType: 'objectId',
description: 'must be an objectid and is required'
}
}
}
}
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment