代码之家  ›  专栏  ›  技术社区  ›  elw

MongoDB mongoose向数组添加数据

  •  0
  • elw  · 技术社区  · 6 年前

    我正试图通过mongoose的post请求将数据添加到数组中。我可以用Model.find()找到正确的数据。但我不知道如何在每个post请求上向“guests”数组添加数据。例如,我希望来宾返回这样的内容(可以在post请求中添加来宾):

    guests: [{
        id: 12412,
        naam: 'John',
        email: 'john@doe.com'
      },
      {
        id: 12412,
        naam: 'John',
        email: 'john@doe.com'
      }
    ]
    

    var express = require('express');
    var mongoose = require('mongoose');
    var Schema = mongoose.Schema;
    const bodyParser = require("body-parser");
    
    var app = express();
    
    var schemaName = new Schema({
      eventid: String,
      evenementnaam: String,
      locatie: String,
      datum: String,
      tijd: String,
      beschrijving: String,
      naam: String,
      email: String,
      telefoon: String,
      image: String,
      time: Number,
      guests: [{
        userid: Number,
        naam: String,
        email: String
      }] // WANT TO ADD TO THIS ARRAY
    
    }, {
      collection: 'evenementen'
    });
    
    var Model = mongoose.model('Model', schemaName);
    
    mongoose.connect('mongodb+srv://db');
    
    app.post('/addguest', function(req, res) {
    
      req.body.eventid;
    
      mongoose.connection.on('open', function(err, doc) {
        console.log("connection established");
    
        mongoose.connection.db.collection('evenementen', function(err, docs) {
    
          Model.find({
            'eventid': req.body.eventid;
          }, function(err, result) {
            if (err) throw err;
            if (result) {
              // ADD GUEST TO GUESTS ARRAY
            } else {
              res.send(JSON.stringify({
                error: 'Error'
              }))
    
            }
          })
    
    
        });
      });
    });
    

    更新#1 下面的测试没有给“guests”添加任何内容

    mongoose.connection.on('open', function (err, doc) {
    console.log("connection established");
    
    var newdata = [{
        userid: 21424,
        naam: 'John Test',
        email: 'test@test.com'
    }];
    
    mongoose.connection.db.collection('evenementen', function (err, docs) {
    
        Model.findOne({
            'eventid': 'srA4aqC'
        }, function (err, result) {
            if (err) throw err;
            if (result) {
                console.log(result);
                Model.update({
                    'eventid': 'srA4aqC'
                }, {
                    '$push': {
                        'guests': {
                            '$each': newdata
                        }
                    }
                })
                console.log('succes');
            } else {
                res.send(JSON.stringify({
                    error: 'Error'
                }))
    
            }
        })
    
    
    });
    });
    

    需要从几个元素中删除''。正确代码:

    $push: {
        guests: {
              $each: newdata
            }
    }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Amit Dimri    6 年前

    首先如果你的 eventid Model.find() Model.findOne() 这将更好,然后更新一个数组字段,您可以使用 $push operator

    if (result) {
        Model.update({'eventid': req.body.eventid}, {'$push': {'guests': result}})
    }
    

    但是,如果您有一个这样的来宾值,则可以执行此操作:

    {
        userid: Number,
        naam: String,
        email: String
    }
    

    但是你有 guests $each 操作员:

    if (result) {
        Model.update({'eventid': req.body.eventid}, {'$push': {'guests': {'$each': result}}})
    }