数组类型值在nodejs和Mongodb中的插入

时间:2019-07-18 07:39:44

标签: arrays node.js mongoose

我正在尝试插入数组类型值,但是它在数据库中提供了空白数组,并且在我使用req.body时也提供了空白数组。请帮忙。 这是我用来在数据库中插入值的方法。

dealExpiryDate: req.body.dealExpiryDate,
dealExpiryTime: req.body.dealExpiryTime,
availableCoupon: req.body.availableCoupon,
peopleLimit: req.body.peopleLimit,
"$push": {
    dealType: req.body.dealType,
    subDealTittle: req.body.subDealTittle,
    description: req.body.description
}

1 个答案:

答案 0 :(得分:0)

您可以直接插入一个数组,如下所示

View

结果:

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/test', {
  useNewUrlParser: true
});
mongoose.set('debug', true);

var messageSchema = new mongoose.Schema({

  content: { type: String },
  likes: [Number]
});

var Message = mongoose.model('Message', messageSchema);

var array = [];
for (var i = 0; i < 10; ++i) {
  array.push(i);
}

var msg = {
  content: "test message",
  likes: array,
}

Message.create(msg).
  then(message => {
    console.log(message);
    process.exit(0);
  });