Skip to content

Instantly share code, notes, and snippets.

View pablocattaneo's full-sized avatar

Pablo Cattaneo pablocattaneo

View GitHub Profile
// npm install --save body-parser
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
//This example shows a middleware function with no mount path. The function is executed every time the app receives a request.
app.use(bodyParser.urlencoded({extended:false})) // Parse x-form-www-urlencoded
const express = require('express')
const app = express()
app.get('/', (req, res) => {
res.send('Hello Mundo!')
})
app.listen(3000)
@pablocattaneo
pablocattaneo / db.js
Last active January 8, 2019 11:54
Utility setup
const mongodb = require('mongodb')
const MongoClient = mongodb.MongoClient
const mongoDbUrl = 'mongodb://localhost/database_name' // Standard URI connection scheme: mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
let _db
const initDb = callback => {
@pablocattaneo
pablocattaneo / how_to_delete_all_documents_into_a_collection_in_mongodb.js
Created January 7, 2019 12:21
How to delete all documents into a collection in mongodb? #mongoDb #mongoShell
db.collectionName.deleteMany({})
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);
// Example:
new Date(1982,08,10)
// note that monthIndex Integer value representing the month, beginning with 0 for January to 11 for December.
db.collectionName.drop()
@pablocattaneo
pablocattaneo / list_collection_mongodb.zsh
Created January 7, 2019 11:14
How to list all collection in mongoDb? #mongoDb #mongoShell
show collections
@pablocattaneo
pablocattaneo / validate_collection_mongodb.js
Last active January 7, 2019 11:52
How to add collection validation in MongoDb? #mongoDb #mongoDbCollectionValidation #mongoShell Source: 1. https://www.udemy.com/mongodb-the-complete-developers-guide/learn/v4/t/lecture/11758316?start=0 2. https://docs.mongodb.com/manual/core/sche
db.createCollection("collectionName", {
validator: {
$jsonSchema: {
bsonType: "object", // everything in the collection should be a valid Mongo Document
required: ["fileld1", "fiel2""],
properties: {
fileld1: {
bsonType: "bsonType",
description: "description"
}
const mongodb = require('mongodb').MongoClient;
mongodb.connect('mongodb://localhost/database_name', {useNewUrlParser: true })
.then(( client ) => {
console.log('Connected!')
client.close()
})
.catch(err => {
console.log(err)
})
npm install mongodb --save