Di sini kode sisi server saya

const mongoose = require("mongoose");
    const Schema = mongoose.Schema;
    
    const restaurantSchema = new Schema({
        address: {
            building: String,
            coord: [Number],
            street: String,
            zipcode: String
        },
        borough: String,
        cuisine: String,
        grades: [{
            date: Date,
            grade: String,
            score: Number
        }],
        name: String,
        restaurant_id: String
    });
    
    module.exports = class RestaurantDB{
        constructor(connectionString){
            this.connectionString = connectionString;
            this.Restaurant = null;
        }
    
       
        async addNewRestaurant(data){
            let newRestaurant = new this.Restaurant(data);
            await newRestaurant.save();
            return `new restaurant: ${newRestaurant._id} successfully added`
        }
        

Saya baru di MongoDB. Jadi bisakah Anda memberi tahu saya bagaimana saya menambahkan data baru ke Atlas MongoDB saya menggunakan "addnewrestaurant ()" node.js

0
Nirodha Wickramarathna 28 Mei 2021, 23:41

1 menjawab

Jawaban Terbaik

Anda dapat memanggil addnewrestaurant dengan data apa pun yang Anda inginkan, cocok dengan skema Anda.

import RestaurantDB from "./RestaurantDB.js";

var rdb = new RestaurantDB('xxxxx'); // pass here the connection string

// then the data to write
await rdb.addNewRestaurant({
  name: 'test'
})
1
Raphael PICCOLO 28 Mei 2021, 20:52