2014-05-16 15 views
0

我這樣定義如何使子文件獨特

{ 

    'info' : { 
     'type' : 'dict', 
     'unique' : True, 
     'schema' : { 
      'propertyA' : {'type':'string'}, 
      'propertyB' : {'type':'string'}, 
      'propertyC' : {'type':'string'} 

     } 
    }, 

    'others' : {'type':'string'} 
} 

架構之後,我發佈了以下文件兩次,第一次返回OK,而第二次則返回「不是唯一的錯誤」。

[ 
    { 
    "info" : { 
     "propertyA" : "a", 
     "propertyB" : "b", 
     "propertyC" : "c" 
    }, 
    "others" : "other things" 
    } 
] 

但是當我發佈一個文檔列表如下:

[ 
    { 
    "info" : { 
     "propertyA" : "a", 
     "propertyB" : "b", 
     "propertyC" : "c" 
    }, 
    "others" : "other things" 
    }, 
    { 
    "info" : { 
     "propertyA" : "a", 
     "propertyB" : "b", 
     "propertyC" : "c" 
    }, 
    "others" : "other things" 
    } 
] 

兩個文件插入到數據庫中。

爲什麼他們有不同的結果?

回答

0

發生這種情況的原因是,在第二種情況下,兩個文檔將使用一個回送存儲到數據庫,從而利用MongoDB批量插入功能。因此,當第二個文檔針對數據庫進行驗證時,尚未找到重複的內容。

在第一種情況下,您正在執行兩項單獨的保存操作,因此第二種情況會匹配重複項。