2015-04-07 60 views
0

我有一個貓鼬模式模型,看起來像下面的示例。帳戶字段是一組帳戶ID。添加到Mongoose模式中的數組

var ListingSchema = new Schema({ 
    listing: { type: String, required: true }, 
    admin: { type: String, required: true, index: true }, 
    accounts: [{ type: String, index: true }] 
}); 

現在在我的節點/快遞後臺,我添加一個條目,像這樣:

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}); 
}); 

但是我不添加到賬戶場正確,因爲它是一個數組。我怎樣才能做到這一點?

回答

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' } 
+0

該創建似乎不適用於我的陣列... – Coop

1
var accounts = [req.session.userID]; 

而且

accounts: [{ type: String, index: true }] 

應該

accounts: [{ type: Array, index: true }]