2013-02-11 72 views
0

我對mongodb非常陌生,我想知道何時添加嵌入式文檔,如果有方法我只能檢查person_Id而不是活動字段。我只關心爲person_id的不重複在Mongodb中,如何檢查嵌入文檔中的重複內容

collection.update({'_id':new BSON.ObjectID(business_id)}, {$addToSet: {members : {person_Id : person_id, active : true }}}, {safe:true}, function(err, result) { 
     if (err) { 
      console.log('Error updating person: ' + err); 
     } else { 
      console.log('' + result + ' document(s) updated'); 
      callback(result); 
     } 
    }); 
+0

http://stackoverflow.com/questions/4435637/mongodb-unique-key-in-embedded-document的可能重複 – aga 2013-02-11 06:26:08

回答

0

如果我理解正確你的問題,那麼答案是肯定的。你可以把你的查詢您的成員不應該包含一個成員與該特定爲person_id的標準:

update({'_id':new BSON.ObjectID(business_id), 'members.person_id':{$ne:person_id}}, {$push: {members : {person_Id : person_id, active : true }}} 

這將推動新的成員記錄到你的成員陣列,當且僅當business_id比賽和成員數組不包含具有person_id的成員。請注意,這種方法與upserts相互排斥。

相關問題