Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
1.4 kB
5
Indexable
Never
const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/relationshipDB', {

})
    .then(() => {
        console.log("MONGO CONNECTION OPEN!!!")
    })
    .catch(err => {
        console.log("OH NO MONGO CONNECTION ERROR!!!!");
        console.log(err);
    })

const userSchema = new mongoose.Schema({
    first: String,
    second: String,
    addresses: [
        {
            address: {
                type: String,
                required: true
            },
            city: {
                type: String,
                required: true
            },
            state: {
                type: String,
                required: true
            },
            country: {
                type: String,
                required: true
            }
        }
    ]
})

const User = mongoose.model('User', userSchema)

const makeUser = async () => {
    const u = new User({
        first: "Harry",
        last: "Potter",
        adresses: [
            {
                street: "123 Sesame Street",
                city: "New York",
                state: "NY",
                country: "USA"
            }
        ]

    })

    const res = await u.save()
    console.log(res);
}

makeUser();


Tebahs-MBP:44_Mongo Relationships tebahsaboun$ node Models/users.js 
MONGO CONNECTION OPEN!!!
{
  first: 'Harry',
  _id: new ObjectId("644c01bac512c112b61135ee"),
  addresses: [],
  __v: 0
}