Skip to content

Instantly share code, notes, and snippets.

@parkerfoshay
Last active December 15, 2022 16:45
Show Gist options
  • Save parkerfoshay/9eeaa7430ba99ee2d64840802bd08792 to your computer and use it in GitHub Desktop.
Save parkerfoshay/9eeaa7430ba99ee2d64840802bd08792 to your computer and use it in GitHub Desktop.
mongoose example
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