我正在将nodejs用于服务器端。 这是我的mongodb模式:
const UserSchema = new Schema({
Username: {
type: String,
required: true,
unique: true
},
Password: {
type: String,
required: true
},
Auction: {
Title: {
type: String
},
Description: {
type: String
},
MinimumOfParticipants: {
type: Number
},
NumberOfParticipants: {
type: Number
},
StartingPrice: {
type: Number
},
StartingTime: {
type: Date
},
image: {
type: String
}
},
Participation: {
Title: {
type: String
},
Description: {
type: String
},
StartingPrice: {
type: Number
},
StartingTime: {
type: Date
}
}
});
const User = mongoose.model("user", UserSchema);
我试图通过在一个仅具有用户名和密码的对象中插入名为Auction的新数据数组来更新文档。当用户注册时,文档仅包含username
和password
,并且在创建拍卖时。它将被添加到他的文档中。
这是我要更新的代码,但是不起作用。
var user = req.body.Username;
User.update({
Username: user
}, {
$set: {
Auction: {
Title: req.body.Title,
Description: req.body.Description,
MinimumOfParticipants: req.body.MinimumOfParticipants,
StartingPrice: req.body.StartingPrice,
StartingTime: req.body.StartingTime,
image: req.body.image
}
}
});
res.end();