我正在尝试插入数组类型值,但是它在数据库中提供了空白数组,并且在我使用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
}
答案 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);
});