Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mongodb字段组合唯一_Mongodb_Indexing - Fatal编程技术网

mongodb字段组合唯一

mongodb字段组合唯一,mongodb,indexing,Mongodb,Indexing,我需要确保某些字段的组合是唯一的,所以我发现我们可以使用索引来实现它,比如: db.user.ensureIndex({'fname':1,'lname':1},{unique:true}) 但是在我的场景中,我允许用户添加电子邮件或电话,甚至两者都可以。那么,在这种情况下,我如何实现唯一性呢。我试着设置2个索引 db.user.ensureIndex({'fname':1,'lname':1,'email':1},{unique:true}) 及 当我尝试添加一个电子邮件唯一的文档时,这个

我需要确保某些字段的组合是唯一的,所以我发现我们可以使用索引来实现它,比如:

db.user.ensureIndex({'fname':1,'lname':1},{unique:true})
但是在我的场景中,我允许用户添加电子邮件或电话,甚至两者都可以。那么,在这种情况下,我如何实现唯一性呢。我试着设置2个索引

db.user.ensureIndex({'fname':1,'lname':1,'email':1},{unique:true})

当我尝试添加一个电子邮件唯一的文档时,这个功能就起作用了,如下所示

> db.user.insert({fname:'tony',lname:'stark',email:'aa@gmail.com'})                                                                                 
WriteResult({ "nInserted" : 1 })
> db.user.insert({fname:'tony',lname:'stark',email:'xyz@gmail.com'})                                                                             
WriteResult({                                                                                                                             
    "nInserted" : 0,                                                                                                                  
    "writeError" : {                                                                                                                  
            "code" : 11000,                                                                                                           
            "errmsg" : "E11000 duplicate key error index: temp.user.$fname_1_lname_1_mobile_1 dup key: { : \"tony\", : \"stark\", : null }"                                                                                                                                     
    }                                                                                                                                 
 }) 
我猜错误是因为第二个索引,因为这两个插入对于mobile字段都有null值。然后我试了这个

db.user.ensureIndex({'fname':1,'lname':1,'email':1,'mobile':1},{unique:true})
这很有帮助,有没有办法用mongodb实现这一点,或者用代码检查唯一性

更新 我也需要确保这种情况下的唯一性:

db.user.insert({fname:'tony',lname:'stark',email:'abc@gmail.com'})
<<insert>> 
db.user.insert({fname:'tony',lname:'stark',email:'abc@gmail.com',mobile:554545435}) 
<<dont insert>>
以下是查询:

    >db.users.insert({fname:"tony",lname:"stark",mobile:"123",email:"123@gmail.com"})
WriteResult({ "nInserted" : 1 })
>db.users.insert({fname:"tony",lname:"stark",mobile:"123",email:"123@gmail.com"})
WriteResult({ "nInserted" : 1 })
Mongo版本-v3.2.0


我对mongodb没有太多经验,也没有在其他db中使用索引,我想这是一个noob问题。提前谢谢你

电子邮件
手机
上添加单独的唯一索引怎么样?是的,我发现我可以在这里使用唯一的部分索引。你想用
$gt做什么{$type:10}
子句在部分索引中?在
email
mobile
上添加单独的唯一索引怎么样?是的,我发现我可以从这里使用唯一的部分索引。您试图在部分索引中使用
$gt:{$type:10}
子句做什么?
> db.users.createIndex({ "fname": 1, "lname": 1, "email":1 }, 
... { unique: true, partialFilterExpression:{ 
...   email: { $exists: true, $gt : { $type : 10 } } } })
{
    "createdCollectionAutomatically" : true,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}
> db.users.createIndex({ "fname": 1, "lname": 1, "mobile":1 }, 
... { unique: true, partialFilterExpression:{ 
...   mobile: { $exists: true, $gt : { $type : 10 } } } })
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 2,
    "numIndexesAfter" : 3,
    "ok" : 1
}
    >db.users.insert({fname:"tony",lname:"stark",mobile:"123",email:"123@gmail.com"})
WriteResult({ "nInserted" : 1 })
>db.users.insert({fname:"tony",lname:"stark",mobile:"123",email:"123@gmail.com"})
WriteResult({ "nInserted" : 1 })