repl.it
@426729/

Testing Mongo

Nodejs

No description

fork
loading
Files
  • index.js
  • Packager files
  • package-lock.json
  • package.json
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const express=require("express");
const mongoose=require("mongoose");

const POSTBIN_URL="https://postb.in/1560257312860-3234323128126";

const Schema=mongoose.Schema;

const request=require("request-promise");
// request(POSTBIN_URL);
/*
After you run the above line, go to the Postbin and see what IP the requests came from.
You will whitelist this IP on your MongoDB Atlas server.
*/
mongoose.connect(process.env.DATABASE.replace(/<password>/,process.env.PASSWORD),{useNewUrlParser:true});
const db=mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  const students=new Schema({
    firstName:String,
    lastName:String
  });
  const classes=new Schema({
    name:String,
    participants:[students]
  });
  students.add({
    classes:[classes]
  });
  students.methods.toString=function(){
    return `${this.lastName}, ${this.firstName}`;
  };
  const Student=mongoose.model('Student',students);
  const Class=mongoose.model('Class',classes);
  let physics=new Class({name:"Physics"});
  let me=new Student({firstName:'Andrew',lastName:'Healey'});

  me.save((err,person)=>{
    if(err) return console.error(err);
    console.log("- "+person.toString());
  });
  Student.find({lastName:/ee/},(err,students)=>{
    if(err) console.error(err);
    console.log("-> "+students[0]);
  });

});
?