【发布时间】:2018-01-22 08:14:31
【问题描述】:
我有一个followig复合索引:
db.nodes.createIndex( { parent: 1, name: 1 }, { unique: true } );
该索引禁止插入两个具有相同名称和父级的文档 例如:
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"});
【问题讨论】: