Data transfer and recovery of MongoDB advanced applications

1. MongoDB index

1.1. Create index

db.books.ensureIndex{{number:1}}

Create an index and specify the name of the index

db.books.ensureIndex({number:1},{name:"book_"})

1.2 points needing attention in index use

1) When creating an index, note that 1 is creating an index in positive order - 1 is creating an index in reverse order

2) Index creation will affect the insertion performance of colleagues who improve the query performance. For documents that query frequently and insert less, you can consider using index

3) If it meets the index, pay attention to the order of the index

4) Indexing every key does not necessarily improve performance. Indexing is not everything

5) When sorting, if there is a large amount of data, you can also consider adding an index to improve the sorting performance

1.3. Unique index

Resolve that duplicate values cannot be inserted in document books

1.4 eliminate duplicate values

#Then insert the same name The value will report an error
db.books.ensureIndex({name:-1},{unique:true}) 

What if there are duplicate values before the proposed unique index

#Eliminate duplicate values
db.books.ensureIndex({name:1},{name:"book_",unique:true,dropDups:true}) 

1.5. Create index in the background

In order to solve the problem of creating an index lock table, it can be run in the background without affecting the query function

db.books.ensureIndex({name:1},{background:true})

1.6. Forced query of established indexes

#Last one name Is the index name. The positive and reverse order is based on the rules of index creation. Otherwise, an error will be reported
db.books.find({name:"323book"}).hint({name:1}) 

1.7. Check the established index of the database in the shell

db.system.indexes.find()
db.system.namespaces.find()

1.8 query index information and query status information

db.books.find({name:"123book"}).explain()

1.9 batch and precise deletion index

db.runCommand({dropIndexes : "books" , index:"name_-1"})
db.runCommand({dropIndexes : "books" , index:"*"})

2. Two dimensional index

Building a two-dimensional index

#One will be created by default[-108,108]Scope of
db.map.ensureIndex({gis:"2d"},{min:-1,max:201}) 

3. MongoDB data transfer and recovery

3.1. Export data (interrupt other operations)

Using the mongoexport command line

-d indicates the library used

-c indicates the table to export

-o indicates the file name to export

-csv specifies the exported csv format

-q filter export

--type< json|csv|tsv>

Export the data to persons in testdb

mongoexport -d testdb -c persons -o D:/persons.json

Export documents from other host databases

mongoexport --host 192.168.0.16 --port 37017

3.2. Import data (interrupt other operations)

mongoimport --db testdb --collections persons --file d:/persons.json

3.3. Run time backup mongodump exe

API: http://docs.mongodb.org/manua...

mongodump --host 127.0.0.1:27017 -d testdb -o d:/testdb

3.4. Restore mongorestore at runtime exe

API:http://docs.mongodb.org/manua...

Restore database

db.dropDatabase()
mongorestore --host 127.0.0.1:27017 -d testdb -directoryperdb d:/testdb/testdb

MongoDB is a file database, which can be backed up by copying files

3.5 locking and unlocking

db.runCommand({fsync:1,lock:1}) #Lock
db.currentOp() #Unlock

3.7 data repair

When an irreversible disaster such as power failure comes, garbage data will be generated due to the storage structure of mongodb. After data recovery, the garbage data still exists. This is a database, which can provide a self-healing ability It's easy to use

    
db.repairDatabase()

Pay attention to WeChat official account Tom structure and reply to "MongoDB" to get supporting information.

This article is the original of "Tom bomb architecture". Please indicate the source for reprint. Technology lies in sharing, I share my happiness! If you have any suggestions, you can also leave comments or private letters. Your support is the driving force for me to adhere to my creation. Focus on WeChat official account Tom structure, get more dry cargo!

It's not easy to be original. It's cool to insist. I've seen it here. Little partners remember to like, collect and watch it. Pay attention to it three times a button! If you think the content is too dry, you can share and forward it to your friends!

Keywords: Java MongoDB

Added by Malkavbug on Fri, 07 Jan 2022 07:45:29 +0200