Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongo createIndex background blocks the shell

Tags:

mongodb

I am creating an index on a few Gigabits of documents.

In my mongo shell, I ran this command:

db.positions.createIndex( { "imei": 1, "server_date": -1 }, { "background": true, "name": "positions_imei_server_date" } );

However I did not get the shell prompt back even with background set at true.

If I look in the log, I see:

[conn13]   Index Build (background): 14129300/19197181 73%

So it seems that it is really running as a background task. However why did I not get back the prompt in the shell? Is it normal? If I type CTRL+C will it cancel my index?

I am running the mongo 3.4.0

like image 911
jobou Avatar asked Dec 08 '16 14:12

jobou


People also ask

What is createIndex in MongoDB?

The indexes are ordered by the value of the field specified in the index. So, MongoDB provides a createIndex() method to create one or more indexes on collections. Using this method we can create different types of indexes like text index, 2dsphere index, 2d index, etc.

What is background index in MongoDB?

Fortunately, MongoDB has an option to build indexes in the background. It means MongoDB can still serve queries and you can alter the database meanwhile it is building the index.

What is the interactive shell for MongoDB called?

mongo is an interactive JavaScript shell interface to MongoDB, which provides a powerful interface for system administrators as well as a way for developers to test queries and operations directly with the database.

How to create Index in the background in MongoDB?

MongoDB Database Big Data Analytics To create index in the background, use createIndex () method and set “background: true” as in the below syntax − db.yourCollectionName.createIndex ({"yourFieldName1":1,"yourFieldName2":1}, {background: true}); Let us implement the above syntax in order to create index and set background −

Does the foreground option block the write operation in MongoDB?

When building an index on a collection, the database that holds the collection is unavailable for read or write operations until the index build completes. The unique index failed to build. This result showed the foreground option didnot block the write operation in MongoDB.

What are index types in MongoDB?

A document that contains the field and value pairs where the field is the index key and the value describes the type of index for that field. For an ascending index on a field, specify a value of 1; for the descending index, specify a value of -1. MongoDB supports several different index types including text, geospatial, and hashed indexes.

How to create a case-insensitive Index in MongoDB?

If you do specify a collation when creating the index, MongoDB creates the index with the specified collation. By specifying a collation strength of 1 or 2, you can create a case-insensitive index. Index with a collation strength of 1 is both diacritic- and case-insensitive.


Video Answer


1 Answers

This is the expected behavior. background means that operations like listing collection in the database you're building the index on won't block. However the shell command you started remains running. You can simply open another shell and connect to the server. Issuing CTRL+C will abort the index creation.

See more here: https://docs.mongodb.com/v3.0/core/index-creation/#background-construction

like image 73
Meni Avatar answered Oct 24 '22 01:10

Meni