15

私はMongoDBの初心者で、いくつか試してみています。URLを保存し、URLの重複を避けるために、URLに一意のインデックスを作成します。そのように

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

しかし、プログラムを起動するたびに、インデックスが再度作成されますね。

これは、プログラムが1つの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}

ありがとう

編集 :

これが、MongoDBimportjava.net.UnknownHostExceptionを処理する私のクラスMongoです。インポートjava.util.List; import java.util.Set;

インポートcom.mongodb.BasicDBObject; インポートcom.mongodb.DB; インポートcom.mongodb.DBCollection; インポートcom.mongodb.DBObject; インポート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<String> colls = db.getCollectionNames();

        for (String s : colls) {
            System.out.println(s);
        }
    }

    public void listIndex(){
         List<DBObject> 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();
        }

    }
}
4

5 に答える 5

22

一意の値を文字列ではなくブール値trueとして渡す必要があります。これは、オプションである2番目のパラメーターです。

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

また、mongoインタープリターを使用して手動でテストしました。

> db.createCollection("sa")
{ "ok" : 1 }
> db.sa.ensureIndex({"url":1},{unique:true})
> db.sa.insert({url:"http://www.example.com", crawled: true})
> db.sa.insert({url:"http://www.example.com", crawled: true})
E11000 duplicate key error index: test.sa.$url_1  dup key: { : "http://www.example.com" }
> db.sa.insert({url:"http://www.example2.com/", crawled: false})
> db.sa.insert({url:"http://www.example.com", crawled: false})
E11000 duplicate key error index: test.sa.$url_1  dup key: { : "http://www.example.com" }
>

オブジェクトは2つだけです。

> db.sa.find()
{ "_id" : ObjectId("50d636baa050939da1e4c53b"), "url" : "http://www.example.com", "crawled" : true }
{ "_id" : ObjectId("50d636dba050939da1e4c53d"), "url" : "http://www.example2.com/", "crawled" : false }
于 2012-12-22T22:48:24.090 に答える
2

私はあなたの問題を完全には理解していませんが、後者は常にインデックスを作成しようとしますが、前者はそれが存在することを確認ensureIndexするだけなので、代わりに使用する必要がある可能性が非常に高いと思います。createIndex

于 2012-12-22T20:49:09.550 に答える
2

この質問に出くわしたばかりで、バージョン3.0.0以降にいくつかの変更があります。

db.collection.ensureIndex(keys, options)

バージョン3.0.0以降で非推奨:db.collection.ensureIndex()はdb.collection.createIndex()のエイリアスになりました。

インデックスがまだ存在しない場合は、指定されたフィールドにインデックスを作成します。

于 2015-08-27T21:35:50.987 に答える
0

mongodbの一意のインデックスを使用するには、2つのパラメーターを使用するメソッドを使用する必要があります。3番目のブールパラメーターは「一意の」インデックス用です。

mongo.getCollection()。ensureIndex(new BasicDBObject( "url"、1)、 "unq_url"、true));

于 2013-11-08T12:02:50.180 に答える
0

また、getCollection()で指定されたコレクション名がないことがわかります。

それはどのコレクションを選択しますか?奇妙な

于 2014-11-07T16:47:41.723 に答える