Create an index with MongoDb

前端 未结 5 1723
春和景丽
春和景丽 2021-02-13 03:20

I\'m beginner with MongoDB and i\'m trying some stuff. I want to store URL and to avoid duplicate URL I create an unique index on the url. Like that

collection.c         


        
5条回答
  •  一向
    一向 (楼主)
    2021-02-13 04:10

    To Use the unique index of the mongodb, you should use the method with 2 parameters where 3rd boolean parameter is for the "unique" index.

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

提交回复
热议问题