确保索引不起作用

编程入门 行业动态 更新时间:2024-10-25 04:22:23
本文介绍了确保索引不起作用 - MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试确保文档字段的索引,而不是工作。 MongoDB版本是2.0.2。 这是非常基本的,不知怎的,我觉得我可能错过了的东西或不! 能够插入3个带有相同空字符串的文档!!

I am trying to ensure index on a field of a document and its not working. MongoDB version is 2.0.2. This is very basic and somehow i'm feeling i might have missed something or not!!! Its able to insert 3 docs with same empty string !!

以下是详细信息:

> use testinsert switched to db testinsert > db.users.ensureIndex({ name : 1}); > doc = { ... name : "" ... } { "name" : "" } > doc { "name" : "" } > db.users.stats(); { "ns" : "testinsert.users", "count" : 0, "size" : 0, "storageSize" : 8192, "numExtents" : 1, "nindexes" : 2, "lastExtentSize" : 8192, "paddingFactor" : 1, "flags" : 1, "totalIndexSize" : 16352, "indexSizes" : { "_id_" : 8176, "name_1" : 8176 }, "ok" : 1 } > db.users.insert(doc); > db.users.insert(doc); > db.users.insert(doc); > db.users.find(); { "_id" : ObjectId("4f1980dc7e154e6702c4914c"), "name" : "" } { "_id" : ObjectId("4f1980dd7e154e6702c4914d"), "name" : "" } { "_id" : ObjectId("4f1980dd7e154e6702c4914e"), "name" : "" } > db.users.ensureIndex({ name : 1}); > db.users.insert(doc); > db testinsert > db.users.find(); { "_id" : ObjectId("4f1980dc7e154e6702c4914c"), "name" : "" } { "_id" : ObjectId("4f1980dd7e154e6702c4914d"), "name" : "" } { "_id" : ObjectId("4f1980dd7e154e6702c4914e"), "name" : "" } { "_id" : ObjectId("4f1981557e154e6702c49150"), "name" : "" } > version(); version: 2.0.2

有人可以澄清可能是什么问题!

can somebody pls clarify as to what might be the problem !

推荐答案

没有问题,你可以看到索引在那里。你期望它不允许重复元素?为此,您必须将唯一标志设置为true:

There are no problems, you can see that the index is there. You're expecting it not to allow duplicate elements? For that you have to set unique flag to true:

db.users.ensureIndex({ name : 1},{unique: true});

更新:使用 {unique:true}再次运行确保不起作用,你必须再次放弃 ensureIndex :

UPDATE: running ensure once again with {unique: true} doesn't work, you have to drop and ensureIndex again:

db.users.dropIndex({name:1}) db.users.ensureIndex({ name : 1}, {unique:true, dropDups : true});

更多推荐

确保索引不起作用

本文发布于:2023-11-22 13:04:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617522.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   索引

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!