Last active
December 15, 2022 16:45
-
-
Save parkerfoshay/9eeaa7430ba99ee2d64840802bd08792 to your computer and use it in GitHub Desktop.
mongoose example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require('mongoose'); | |
main().catch(err => console.log(err)); | |
async function main() { | |
await mongoose.connect(<Atlasconnection-string>); | |
const userSchema = new mongoose.Schema({ | |
name: { | |
type: String, | |
required: true, | |
}, | |
phone_number: String, | |
age: Number, | |
addresses: [ | |
{ | |
street: String, | |
city: String, | |
state: String, | |
} | |
], | |
favorites: Array | |
}); | |
const User = mongoose.model('User', userSchema); | |
await User.create({ | |
name: "Mira Chan", | |
phone: "7650984321", | |
age: 88, | |
addresses: [ | |
{street: "123 Main St", city: "New York", state: "NY"}, | |
{street: "456 Broadway", city: "New York", state: "NY"} | |
], | |
favorites: ["Peanut Butter", "Chocolate", "Sherbert"] | |
} | |
); | |
// Find all customers | |
const docs = await User.find(); | |
console.log(docs); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment