2014-06-05 55 views
0

我有以下貓鼬架構(I去除一些不必要的比特):刪除的子子陣列(所有元素)

var VoteSchema = new Schema({ 
    voter: String, 
    val: {type:Number, max:1, min:-1}, 
    voted_at: { type: Date, default: Date.now }, 
    comments: [{type:String, trim:true}] 
}); 

var LexicalEntrySchema = new Schema({ 
    label:{type:String,trim:true, index:true}, 

    images:[{ 
     image_uri: {type:String,trim:true}, 
     description: String, 
     votes:[ { type: Schema.ObjectId, ref: 'Vote'} ] 
    }], 
}); 

我要刪除中的所有圖像的所有票所有詞彙條目。這怎麼能做到?

+1

你有試過任何東西嗎?因爲你問的方式聽起來像是你想讓別人在這裏爲你解決問題,而不是你想克服一些障礙來自己做。 –

回答

0

你需要做三個查詢:

  1. 與你需要的任何過濾器LexicalEntry.find。使用{images.votes: 1}作爲投影參數。
  2. 執行LexicalEntry.update並更新步驟1中找到的所有匹配文檔。{$set: {"images.votes": []}}
  3. 刪除遺留的投票文件:Votes.remove與所有的ID,你在步驟1中

正如你可能已經猜到雲集,這是相當次優的。通過做

var LexicalEntrySchema = new Schema({ 
    label:{type:String,trim:true, index:true}, 

    images:[{ 
     image_uri: {type:String,trim:true}, 
     description: String, 
     votes: [ VoteSchema ] 
    }] 
}); 

插入新的投票:你應該充分利用蒙戈的能力來嵌入文檔someEntryDocument.images[0].votes.push({voter: ..., val: ...})

刪除在做投票:someEntryDocument.images[0].votes = []

貓鼬的種羣特徵最適合多種關係。