Cara menggunakan geospatial indexing mongodb

By On Thursday, October 13th, 2022 Categories : Tanya Jawab

Cara menggunakan geospatial indexing mongodb – Halo sista dan agan semua, Terima Kasih sudah berkunjung ke web site Trend Wisata ini. Sore ini, saya di web site trendwisata.com ingin sharing tanya jawab yang wokeh yang menjelaskan tentang Cara menggunakan geospatial indexing mongodb. Silahkan sista dan agan simak di bawah ini:

  • Scala Driver
  • Tutorials
  • Create Indexes

Table of Contents

  • Prerequisites
  • Connect to a MongoDB Deployment
  • Ascending Index
  • Single Ascending Index
  • Compound Ascending Index
  • Descending Index
  • Single Descending Key Index
  • Compound Descending Key Index
  • Compound Indexes
  • Text Indexes
  • Hashed Index
  • Geospatial Indexes
  • geoHaystack
  • IndexOptions
  • Unique Index
  • Partial Index
  • Get a List of Indexes on a Collection
  • What is create index in MongoDB?
  • How is indexing done in MongoDB?
  • How many indexes can we create in MongoDB?
  • What is the type of weights parameter in createIndex ()?

Indexes support the efficient execution of queries in MongoDB. To create an index on a field or fields, pass an
index specification document to the MongoCollection.createIndex() method.

The MongoDB Scala Driver provides the Indexes class that includes helper methods to create index specification documents for the various MongoDB Index key types.

Note

MongoDB only creates an index if an index of the same specification does not already exist.

Prerequisites

  • Include the following import statements:

     import org.mongodb.scala._
    
     import org.mongodb.Indexes
     import org.mongodb.IndexOptions
     import org.mongodb.Filters
    

Connect to
a MongoDB Deployment

Connect to a MongoDB deployment and declare and define a MongoDatabase and a MongoCollection instances.

For example, include the following code to connect to a standalone MongoDB deployment running on localhost on port 27017 and define database to refer to the test database and collection to refer to the restaurants collection:

val mongoClient: MongoClient = MongoClient()
val database: MongoDatabase = mongoClient.getDatabase("test")
val collection: MongoCollection[Document] = database.getCollection("restaurants")

For additional information on connecting to MongoDB, see Connect to MongoDB.

Ascending Index

To
create a specification for an ascending index, use the Indexes.ascending helper methods.

Single Ascending Index

The following example creates an ascending index on the name field:

collection.createIndex(Indexes.ascending("name"))
          .printResults()

Compound Ascending Index

The following example creates an ascending compound index on the stars field and the name field:

collection.createIndex(Indexes.ascending("stars", "name"))
          .printResults()

For an alternative way to create a compound index, see Compound Indexes.

Descending Index

To create a specification of a descending index, use the Indexes.descending helper methods.

Single Descending Key Index

The following example creates a descending index on the stars field:

collection.createIndex(Indexes.descending("stars"))
          .printResults()

Compound Descending Key Index

The following example creates a descending compound index on the stars field and the name field:

collection.createIndex(Indexes.descending("stars", "name"))
          .printResults()

For an alternative way
to create a compound index, see Compound Indexes.

Compound Indexes

To create a specification for a compound index, use the Indexes.compoundIndex helper methods.

Note

To create a specification for a compound index where all the keys are ascending, you can use the ascending() method. To create a specification for a compound index where all the keys are descending, you can use the descending() method.

The following example creates a compound index with the
stars field in descending order and the name field in ascending order:

collection.createIndex(
              Indexes.compoundIndex(Indexes.descending("stars"), 
                                    Indexes.ascending("name")))
          .printResults()

Text Indexes

MongoDB provides text indexes to support text search of string content. Text indexes can include any field whose value is a string or an array of string elements. To create a specification for a text index, use the Indexes.text helper method.

The following example creates a text index on the name field:

collection.createIndex(Indexes.text("name"))
          .printResults()

Hashed Index

To create a specification for a hashed index index, use the Indexes.hashed helper method.

The following example creates a hashed index on the _id field:

collection.createIndex(Indexes.hashed("_id"))
          .printResults()

Geospatial Indexes

To support geospatial queries, MongoDB supports various geospatial indexes.

2dsphere

To create a specification for a 2dsphere index, use the Indexes.geo2dsphere static helper methods.

The following example creates a 2dsphere index on the "contact.location" field:

collection.createIndex(Indexes.geo2dsphere("contact.location"))
          .printResults()

geoHaystack

To create a specification for a geoHaystack index, use the Indexes.geoHaystack method. geoHaystack indexes can improve performance on queries that use flat geometries.

The following example creates a geoHaystack index on the contact.location field and an ascending index on the stars field:

val haystackOption = IndexOptions().bucketSize(1.0)
collection.createIndex(
            Indexes.geoHaystack("contact.location", Indexes.ascending("stars")),
            haystackOption)
        .printResults()

To query a haystack index, use the geoSearch command.

IndexOptions

import org.mongodb.scala.model.IndexOptions

In addition to the index specification document, the createIndex() method can
take an index options document, such as to create unique indexes or partial indexes.

The Scala driver provides the IndexOptions class to specify various index options.

Unique Index

The following specifies a unique(true) option to create a unique index on the name and stars fields:

val indexOptions = IndexOptions().unique(true)
collection.createIndex(Indexes.ascending("name", "stars"), indexOptions)
          .printResults()

For more information on unique indexes, see Unique Indexes.

Partial Index

To create a partial index, include a
partialFilterExpression as an index option.

The following example creates a partial index on documents that have status field equal to "A".

val partialFilterIndexOptions = IndexOptions()
             .partialFilterExpression(Filters.exists("contact.email"))
collection.createIndex(
                Indexes.descending("name", "stars"), partialFilterIndexOptions)
          .printResults()

For more information on partial indexes, see Partial Indexes.

Get a List of Indexes on a Collection

Use the listIndexes() method to get a list of indexes. The following lists the indexes on the collection:

collection.listIndexes().printResults()

For other index options, see MongoDB Manual.

What is create index in MongoDB?

MongoDB creates a unique index on the _id field during the creation of a collection. The _id index prevents clients from inserting two documents with the same value for the _id field. You cannot drop this index on the _id field.

How is indexing done in MongoDB?

MongoDB provides a method called createIndex() that allows user to create an index. The key
determines the field on the basis of which you want to create an index and 1 (or -1) determines the order in which these indexes will be arranged(ascending or descending).

How many indexes can we create in MongoDB?

Maximum Ranges. A collection cannot have more than 64 indexes. The length of the index name cannot be longer than 125 characters. A compound index can have maximum 31 fields indexed.

What is the type of weights parameter in createIndex ()?

Explanation: The type of weights parameter in createIndex() is document.

Cara menggunakan geospatial indexing mongodb | admin | 4.5
shopee flash sale gratis ongkir
x