title: How to create collection with fields/variables/indexes using mongo
body: <p>I'm learning mongo from absolute scratch, and i can't for the hell of it figure out how to create a collection with predefined variables. I need to do that in order to have unique mails on my project. Any help would be very appreciated.</p>
  <p>I'm trying to do a basic <code>CRUD</code>, and everything you see in the code is a lot of different tutorials combined into one, so far I'm able to create new users, and obtain a list of all of them, but in order to delete one or edit/update one I really need an unique attribute.</p>
  <p>This is my code if it is useful:</p>
  <pre class="lang-js prettyprint-override"><code>    const express= require ('express');
      var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
      const app = express();
      const cors = require('cors');
      const bcrypt = require('bcrypt');
      const MongoClient = require('mongodb').MongoClient;
      const saltRounds=10;
  
      app.use(cors());
      app.use(express.json());
      app.listen(3001,()=&gt;{
          console.log(&quot;Welcome&quot;);
      });
  
  
  
      // CONEXION BASE DE DATOS
      MongoClient.connect('mongodb+srv://admin:admin@cluster0.bs9d2.mongodb.net/test?retryWrites=true&amp;w=majority'
      ,{ useUnifiedTopology: true }).then(client=&gt;{
  
          console.log('Bienvenido a database mongo');
          const db = client.db('ezbuy-database');
          const usersCollection = db.collection('users');
  
         
  
         
          /*Añade un usuario nuevo a la BD*/
          /*Falta ver como hacer datos unicos, pero no se como*/
           
          app.post('/newuser', (req, res) =&gt; {
              const userObject ={
              
                  name: req.body.name,
                  identification:req.body.identification,
                  email:req.body.email,
                  password:req.body.password,
                  cellphone:req.body.cellphone,
                  addedDate:req.body.addedDate,
                  lastLoginDate:req.body.addedDate,
                  role:req.body.role,
                  speciality:req.body.speciality
                 }
               ;
              bcrypt.hash(userObject.password,saltRounds,(err,hash) =&gt; {
                  if(err){console.log(err)}
                 userObject.password=hash;
                 
                 usersCollection.insertOne(userObject)
                .then(result =&gt; {
                  console.log(result);
                  console.log(&quot;Dato Añadido a mongo  &quot;);
                  res.send('Usuario Creado');
                })
                .catch(error =&gt; console.error(error))
             
              })
              })
              
              /*Devuelve todos los usuarios creados*/
              app.get('/allusers', (req, res) =&gt; {
                db.collection(&quot;users&quot;).find({}).toArray(function(err, result) {
                  if (err) throw err;
                  console.log(result);
                  res.send(result);
                });
  
                app.get('/userbyid',(req,res) =&gt;{
                  db.collection(&quot;users&quot;).findOne({})
                }
                )
              
                })
  
            
      }).catch(console.error)
  </code></pre>