我正在使用Java连接Mongodb。
我会创建一个集合和一个索引,如下所示:
collection = mongoClient.getDB(DB_NAME).getCollection(COLLECTION_NAME)
collection.ensureIndex(new BasicDBObject(['customerReference': 1, 'unique': true]))
当我在Mongo shell中检查时,我看到:
{
"v" : 1,
"key" : { "customerReference" : 1, "unique" : true },
"ns" : "diagnostics.diagnosticData",
"name" : "customerReference_1_unique_"
}
但是我仍然可以插入重复的数据:
{
"_id" : ObjectId("52f3ba8a7d841c01680e0bc5"),
"customerReference" : 3,
"data" : "original data",
"created" : ISODate("2014-02-06T16:38:34.191Z")
}
{
"_id" : ObjectId("52f3ba8a7d841c01680e0bc6"),
"customerReference" : 3,
"data" : "duplicate data",
"created" : ISODate("2014-02-06T16:38:34.194Z")
}
为什么?