我有一个mongoose架构模型,如下例所示。帐户字段是一个帐户ID数组。
var ListingSchema = new Schema({
listing: { type: String, required: true },
admin: { type: String, required: true, index: true },
accounts: [{ type: String, index: true }]
});
现在在我的Node / Express后端,我添加了一个条目:
Listing.create({
listing: req.body.id,
admin: req.session.userID,
account: req.session.userID
}, function (err){
if (err) throw err;
else res.send({success:true});
});
但是我没有正确添加到帐户字段,因为它是一个数组。我怎么能这样做?
答案 0 :(得分:1)
对于创建尝试这个:
var accounts = [req.session.userID];
Listing.create({
listing: req.body.id,
admin: req.session.userID,
accounts: accounts
}, function (err){
if (err) throw err;
else res.send({success:true});
});
有关更新,请尝试以下操作:
{ $push: { accounts: 'value' }
答案 1 :(得分:1)
var accounts = [req.session.userID];
并且
accounts: [{ type: String, index: true }]
应该是
accounts: [{ type: Array, index: true }]