1
我有一個followig化合物索引:MongoDB的化合物稀疏索引
db.nodes.createIndex({ parent: 1, name: 1 }, { unique: true });
該索引forbides至插入兩個文檔具有相同名稱和父 例如:
var n=db.nodes;
n.insert({parent:0,name:"node"});
n.insert({parent:0,name:"node1"});
n.insert({parent:0,name:"node2"});
n.insert({parent:0,name:"node3"});
//throws an error because of compound index:
n.insert({parent:0,name:"node"});
這是確定。現在,如果名稱爲空(或不存在),我想添加多個文檔與同一父(如通過稀疏的單個索引)。它是否可行? 例子:
n.insert({parent:0,otherattr:"test"});
//throws an error because the tupel {parent:0,name:null} already exists
n.insert({parent:0,otherattr2:"test"});
它的工作原理非常感謝 – neoexpert