как правильно индексировать в mongodb? - PullRequest
0 голосов
/ 25 октября 2019

Мне дали этот вопрос для моего университетского задания

Find the title, publisher, publication year and price of the books with a given book title. 

, и у этого вопроса есть свой собственный набор подвопросов

1.) Create an index that speeds up processing of a query consistent with a pattern.

2) Apply a method getIndexes() to list all existing indexes,

3) Apply a method explain()to verify whether the system plans to use the indexes created for processing of a query consistent with a pattern

4)  Drop an index created in Step 1 with a method dropIndex() 

Это была моя попытка ответить на эти вопросы

//task1
db.bookstore.createIndex( {"book.title":1}, 
{"unique":false} );

//task 2
db.bookstore.getIndexes();

//task 3
db.bookstore.find({"book.title": "Algorithms"}).explain(); //uses the index?

//task 4
db.bookshop.dropIndex("indexName");

Все это работает без каких-либо проблем, однако я не уверен, правильно ли я это делаю? Любое понимание приветствуется

JS файл, который содержит информацию о «книгах».

db.bookshop.insert( {
    "_id":"185.3.16",
    "book": {
        "callnum":"185.3.16",
        "isbn":"1-292-06118-9",
        "title":"Database Systems",
        "authors":[
            {
                "fname":"Thomas",
                "lname":"Connolly"},
            { 
                "fname":"Carolyn",
                "lname":"Begg"}
        ],
        "publisher":"Pearson Pty Ltd",
        "year":2015,
        "price":136.99,
        "topic":"Computer Science",
        "description":"This is the 6th edition. You can register online to access the examples",
        "keywords":["Database", "XML", "Distributed"]
        }
});

db.bookshop.insert( {
    "_id":"163.24.12",
    "book": {
        "callnum":"163.24.12",
        "isbn":"1-123-456-810",
        "title":"Core Java",
        "authors":[
            {
                "fname":"Horstmann",
                "lname":"Cornell"}
        ],
        "publisher":"PH Pty Ltd",
        "year":2012,
        "price":142.90,
        "topic":"Computer Science",
        "description":"It covers JAVA programming and JAVA script",
        "keywords":["JAVA", "XML", "Script"]
    }
});

db.bookshop.insert( {
    "_id":"123.45.67",
    "book": {
        "callnum":"123.45.67",
        "isbn":"1-123-456-789",
        "title":"Algorithms",
        "authors":[
            {
                "fname":"James",
                "lname":"Bond"},
            {
                "fname":"Harry",
                "lname":"Potter"},
            {
                "fname":"William",
                "lname":"Stallings"}
        ],
        "publisher":"Pearson Pty Ltd",
        "year":2013,
        "price":65.85,
        "topic":"Computer Science",
        "description":"It contains algorithms and their applications. You can download examples from the website"
    }
});

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...