Node.js 无法获取MongoDB复合多键索引以使用Mongoose

Node.js 无法获取MongoDB复合多键索引以使用Mongoose,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我有一个父项文档和一个子项文档,其中我需要子项字段仅在父项上是唯一的。可以有一百万个John Smiths,但我需要每个家长只有一个John Smith //父对象 const mongoose=require('mongoose') const{Schema}=mongoose const ParentSchema=新架构({ // ... children:[ChildSchema]//嵌入许多 }, { 时间戳:{ // ... } }) ParentSchema.index({ “chi

我有一个
父项
文档和一个
子项
文档,其中我需要子项字段仅在父项上是唯一的。可以有一百万个
John Smiths
,但我需要每个
家长只有一个
John Smith

//父对象
const mongoose=require('mongoose')
const{Schema}=mongoose
const ParentSchema=新架构({
// ...
children:[ChildSchema]//嵌入许多
}, {
时间戳:{
// ...
}
})
ParentSchema.index({
“children.firstName”:1,
“children.lastName”:1
}, {
独一无二:没错,
是的
})
//子对象
const ChildSchema=新架构({
名字:{type:String},
姓氏:{type:String}
}, {
时间戳:{
// ...
}
})

所以你需要将
Child.firstName
Child.lastName
Parent.\u id
组合起来,使之独一无二?@Joe是的,完全是为了扮演魔鬼代言人,如果有人有5个儿子,并且给他们起了相同的名字,会发生什么?像@Joe一样,给出的示例只是为了简化另一个用例,实际上是为了一个定价图,我只是想表现得有趣(而且显然失败了——我会坚持做日常工作)