我正在使用一个Java程序来插入MongoDB,尝试为一个字段创建唯一索引。product_src是我的集合中的一个字段,我想将其设置为唯一索引以避免重复插入。我正在尝试以下代码,但显示语法错误,请问问题出在哪里。
这是一个集合。db.t.ensureIndex({"product_src":1})的行存在问题。
请给我一个在Mongo DB中创建唯一索引的示例代码。
DB db;
try {
sample = new MongoClient("myIP",PORT);
db = sample.getDB("client_mahout");
t = db.getCollection("data_flipkart_in_avoid_duplicate_checking");
System.out.println("enter the system ip");
db.t.ensureIndex({"product_src":1});
} catch (Exception e) {}
这是一个集合。db.t.ensureIndex({"product_src":1})的行存在问题。
请给我一个在Mongo DB中创建唯一索引的示例代码。