MongoDB Create lbs Location Index

Source: Internet
Author: User
Tags create index createindex mongoclient mongodb collection

<dependency> <groupId>org.mongodb</groupId> <artifactid>mongo-java-driver</artifactid > <version>3.2.2</version></dependency><dependency> <groupid>org.apache.commons </groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version></ Dependency> PackageCom.teewoo.mongodb;ImportCom.mongodb.BasicDBObject;ImportCom.mongodb.BasicDBObjectBuilder;Importcom.mongodb.MongoClient;Importcom.mongodb.ServerAddress;Importcom.mongodb.client.FindIterable;Importcom.mongodb.client.MongoCollection;ImportCom.mongodb.client.MongoCursor;Importcom.mongodb.client.MongoDatabase;Importorg.apache.commons.lang3.StringUtils;Importorg.bson.Document;Importorg.bson.types.ObjectId;Importjava.util.ArrayList;Importjava.util.Arrays;Importjava.util.List;/*** MongoDB Basic Operation * *@authorXIAOJF 2016-6-24 15:55:56*/ Public classMongobaseops {Private StaticMongoclient client;//MONGO Client    Static{Client=NewMongoclient (Arrays.aslist (NewServerAddress ("localhost", 27017))); }    /*** Get DB object based on DB name * *@paramName DB *@returnDB Object *@authorXIAOJF 2016-6-24 16:27:56*/     Public Staticmongodatabase getdbbyname (String name) {mongodatabase Mongodatabase=client.getdatabase (name); returnmongodatabase; }    /*** Get MONGO Collection object based on MONGO DB name and MONGO collection name * *@paramdbName MONGO DB name *@paramCollectionName MONGO Collection name *@returnMONGO Collection Object *@authorXIAOJF 2016-6-24 16:36:39*/     Public Staticmongocollection getcollectionbyname (String dbname,string collectionname) {mongocollection<Document> mongocollection =getdbbyname (dbName). GetCollection (CollectionName); returnmongocollection; }    /*** New Document * *@paramdbName MONGO DB name *@paramCollectionName MONGO Collection name *@paramDocument Documents *@authorXIAOJF 2016-6-24 16:36:39*/     Public Static voidInsertdoc (String dbname,string collectionname,document Document) {mongocollection mongocollection=getcollectionbyname (DbName, CollectionName);    Mongocollection.insertone (document); }    /*** Batch New document * *@paramdbName MONGO DB name *@paramCollectionName MONGO Collection name *@paramDocuments Collection *@authorXIAOJF 2016-6-24 16:36:39*/     Public Static voidBatchinsertdoc (String dbName, String collectionname, List documents) {mongocollection mongocollection=getcollectionbyname (DbName, CollectionName);    Mongocollection.insertmany (documents); }    /*** Delete Documents by ID * *@paramdbName MONGO DB name *@paramCollectionName MONGO Collection name *@paramID MONGO document _ID *@authorXIAOJF 2016-6-24 17:07:16*/     Public Static voidDeletebyid (String dbName, string collectionname,string ID) {if(Stringutils.isblank (dbName) | | Stringutils.isblank (CollectionName) | |Stringutils.isblank (ID)) {            return ; } mongocollection mongocollection=getcollectionbyname (DbName, CollectionName); Mongocollection.deleteone (NewBasicdbobject ("_id",NewObjectId (ID))); }    /*** Get Documents by ID * *@paramdbName MONGO DB name *@paramCollectionName MONGO Collection name *@paramID MONGO document _ID *@authorXIAOJF 2016-6-24 17:07:16*/     Public StaticObject getById (String dbName, string collectionname,string ID) {if(Stringutils.isblank (dbName) | | Stringutils.isblank (CollectionName) | |Stringutils.isblank (ID)) {            return NULL; } mongocollection mongocollection=getcollectionbyname (DbName, CollectionName); Finditerable finditerable= Mongocollection.find (NewBasicdbobject ("_id",NewObjectId (ID))); Mongocursor cursor=Finditerable.iterator ();  while(Cursor.hasnext ()) {returnCursor.next (); }        return NULL; }    /*** CREATE INDEX * *@paramdbName MONGO DB name *@paramCollectionName MONGO Collection name *@paramfieldName Field Name *@paramIndextype Index Type *@authorXIAOJF 2016-6-27 13:43:02*/     Public Static voidCreateIndex (String dbName, String collectionname,string fieldname,string indextype) {mongocollection Mongocol Lection=getcollectionbyname (DbName, CollectionName); Mongocollection.createindex (NewBasicdbobject (FieldName, Indextype)); }     Public Static voidMain (string[] args) {//get the MongoDB db objectMongodatabase mongodatabase = Mongobaseops.getdbbyname ("Db_test"); //Delete old MongoDB collectionMongodatabase.getcollection ("Collect_gps"). Drop (); List<Document> documentlist =NewArraylist<document>();  for(inti = 0; i<100;i++) {            //Building a 2d indexed lbs documentDocument doc =NewDocument (); Doc.put ("GPs", Basicdbobjectbuilder.start (). Add ("Lon", 33+i). Add ("Lat", i-33). get ());        Documentlist.add (DOC); }        //Batch Add documents toMongobaseops.batchinsertdoc ("Db_test", "Collect_gps", documentlist); //Create a 2d indexMongobaseops.createindex ("Db_test", "Collect_gps", "GPs", "2d"); }}

MongoDB Create lbs Location Index

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.