Introduction to MongoDB
1.0 - draft
1.0 - draft
  • MongoDB
  • Getting Started
    • General
      • Database? Collection? Schema?
      • RDBMS? NoSQL?
      • Scale-Up ? Scale - out?
      • JavaScript Object Notation
    • MongoDB
      • 간략한 역사,
    • Manipulating Database
      • MongoShell
      • PyMongo
  • CRUD
    • General
      • Explain()
      • mongoimport
    • Create
      • Mongo Shell
      • PyMongo
      • Exercises
    • Read
      • Mongo Shell
      • PyMongo
      • Exercises
    • Update
      • Mongo Shell
      • PyMongo
      • Exercises
    • Delete
      • PyMongo
      • Exercises
    • Challenging Exercises
      • Exercise 1
  • Index with Geospatial & Text
    • Basic Index
      • Syntax
      • Exercises
    • Geospatial Index
      • Syntax
      • Exercises
    • Text Index
      • Syntax
      • Exercises
    • Challenge
  • Aggregate framework
    • Aggregate Framework
      • Basic
      • Exercises
  • Distributed
    • Replication
      • Setup
        • Configuration File
        • Running Servers
        • Initiation
        • Exercises
      • Concerns
        • Read Concern
        • Write Concern
        • Exercises
    • Sharding
      • Getting Started
      • Setup
  • MongoDB 4.0 Features
    • Multi-Document Transaction
      • Transaction?
      • Basic
      • Exercises
  • Administration
    • Untitled
Powered by GitBook
On this page
  • Text Index
  • Text Search Operator

Was this helpful?

  1. Index with Geospatial & Text
  2. Text Index

Syntax

Text Index

  • Create a Text Index on PyMongo Create a text index on one field

    collection.create_index( [ ( 'field', pymongo.TEXT ) ] )
  • Create text indexes on multiple fields

    collection.create_index( [ ( 'field_1', pymongo.TEXT ), ( 'field_2', pymongo.TEXT ) ] )
  • Specify the weight of an indexed

    collection.create_index( 
    	[ ( 'field_1', pymongo.TEXT ), ( 'field_2', pymongo.TEXT ) ],
    	weights={ 'field_1' : 3, 'field_2' : 2 } )

Text Search Operator

  • Search

    collection.find( { '$text': {'$search' : 'VALUE' } } } )
    collection.find( { '$text': {'$search' : ' \"VALUE\" ' } } } )		# Phrase
    collection.find( { '$text': {'$search' : ' \"VALUE_1\" \"VALUE_2\" ' } } } )	# AND
    collection.find( { '$text': {'$search' : '  VALUE_1 -VALUE_2 ' } } } )	# Exclude
  • Search with options

    collection.find( { '$text': {'$search' : 'VALUE', '$caseSensitive' : True } } } )	# Case sensitive
    collection.find( { '$text': {'$search' : 'VALUE', '$language' : 'en' } } } )	# Specify a language
  • Include a score field

    collection.find( { '$text': {'$search' : 'VALUE' } }, { 'score' : { '$meta' : 'textScore' } } ) 
    collection.find( { '$text': {'$search' : 'VALUE' } }, { 'score' : { '$meta' : 'textScore' } } )	\
    		.sort( [ ('score', {'$meta': 'textScore’}) ] )	# With sorting

PreviousText IndexNextExercises

Last updated 5 years ago

Was this helpful?