mongodb java索引_java – 使用MongoDb创建一个索引

我是MongoDB的初学者,我正在尝试一些东西.

我想要存储URL并避免重复的URL我创建一个唯一的索引url.

像那样

collection.createIndex(new BasicDBObject("url", type).append("unique", true));

但是每次启动我的程序时,索引都是再次创建的不是吗?

因为,现在我的程序只插入一个url“http://site.com”,如果我重新启动我的程序这个url是插入再次,如果没有索引.

每次创建索引是处理索引的错误方式?

以下是我的代码示例

mongo.getCollection().ensureIndex(new BasicDBObject("url", 1).append("unique", "true"));

mongo.getCollection().insert(new BasicDBObject("url", "http://site.com").append("crawled", 0));

mongo.getCollection().insert(new BasicDBObject("url", "http://site.com").append("crawled", 0));

并输出:

{ "_id" : { "$oid" : "50d627cf44ae5d6b5e9cf106"} , "url" : "http://site.com" , "crawled" : 0}

{ "_id" : { "$oid" : "50d627cf44ae5d6b5e9cf107"} , "url" : "http://site.com" , "crawled" : 0}

谢谢

编辑:

这是我的MongoDB类MongoDB

import java.net.UnknownHostException;

import java.util.List;

import java.util.Set;

import com.mongodb.BasicDBObject;

import com.mongodb.DB;

import com.mongodb.DBCollection;

import com.mongodb.DBObject;

import com.mongodb.MongoClient;

public class Mongo {

private MongoClient mongoClient;

private DB db;

private DBCollection collection;

private String db_name;

public Mongo(String db){

try {

mongoClient = new MongoClient( "localhost" , 27017 );

this.db = mongoClient.getDB(db);

this.db_name = db;

} catch (UnknownHostException e) {

e.printStackTrace();

}

}

public void drop(){

mongoClient.dropDatabase(db_name);

}

public void listCollections(){

Set colls = db.getCollectionNames();

for (String s : colls) {

System.out.println(s);

}

}

public void listIndex(){

List list = collection.getIndexInfo();

for (DBObject o : list) {

System.out.println("\t" + o);

}

}

public void setCollection(String col){

this.collection = db.getCollection(col);

}

public void insert(BasicDBObject doc){

this.collection.insert(doc);

}

public DBCollection getCollection(){

return collection;

}

public void createIndex(String on, int type){

collection.ensureIndex(new BasicDBObject(on, type).append("unique", true));

}

}

这是我的课程,处理我的程序

public class Explorer {

private final static boolean DEBUG = false;

private final static boolean RESET = false;

private Mongo mongo;

private String host;

public Explorer(String url){

mongo = new Mongo("explorer");

mongo.setCollection("page");

if (RESET){

mongo.drop();

System.out.println("Set RESET to FALSE and restart the program.");

System.exit(1);

}

if (DEBUG) {

mongo.listCollections();

}

this.host = url.toLowerCase();

BasicDBObject doc = new BasicDBObject("url", "http://site.com").append("crawled", 0);

mongo.getCollection().ensureIndex(new BasicDBObject("url", 1).append("unique", true));

mongo.getCollection().insert(new BasicDBObject("url", "http://site.com").append("crawled", 0));

mongo.getCollection().insert(new BasicDBObject("url", "http://site.com").append("crawled", 0));

process();

}

private void process(){

BasicDBObject query = new BasicDBObject("crawled", 0);

DBCursor cursor = mongo.getCollection().find(query);

try {

while(cursor.hasNext()) {

System.out.println(cursor.next());

}

} finally {

cursor.close();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值