MongoDB Indexes and Views










0















I'm having trouble finding much information on MongoDB's use of indexes when querying views. The docs state:




Index Use and Sort Operations



Views use the indexes of the underlying collection.



As the indexes are on the underlying collection, you cannot create,
drop or re-build indexes on the view directly nor get a list of
indexes on the view.



You cannot specify a $natural sort on a view.




aaaand that's about it. I'm trying to figure out how to properly index my underlying collections (based on my experience with SQL DBs) to maximize performance of my app's queries on views.



For example, let's say I have a collection holding Product entities in a products collection and which share deleted and categoryId attributes. On top of that I might create a view productsActive defined as deleted: false , and as such on an index on the products collection: deleted: 1 . Now let's say that I commonly do the following query:



db.productsActive.find(categoryId: 'turntable');


Intuitively I want to use a compound index here on the products table:



db.products.createIndex(deleted: 1, categoryId: 1);


Am I doing it right? I suppose ultimately I'm confused by the doc's statement:




Views use the indexes of the underlying collection.




Given that, can mongo use the compound index to initially build the view and then to subsequently query it? Or is simply deriving the view one step (tied to 1 index) and then the subsequent query on the view a second, distinct query with its own index(es)?










share|improve this question






















  • A view is just an "aggregation pipeline", and that is stated quite prominently in the documentation. As such the exact same rules for index usage apply to a view and to an aggregation pipeline. The very first stage of the pipeline used to create the view is the only thing that can use an index. If you have a "question" then simply show how you created the view and any indexes present on the collection it came from, and show your intended usage of said view.

    – Neil Lunn
    Nov 14 '18 at 21:55












  • @NeilLunn "If you have a "question" then simply show how you created the view and any indexes present on the collection it came from, and show your intended usage of said view." That is literally what I included in my question. Did you read it?

    – Madbreaks
    Nov 14 '18 at 22:47












  • Actually you have not done that at all. There is no view definition here or is there any indication of an intended usage pattern for the created view. This is why you were prompted to provide this information. Being Glib and really quite rude typically does not endear people to "help" you. And most people who have something to ask need help. Perhaps you should start reading what is actually being asked of you and make those points clearer.

    – Neil Lunn
    Nov 14 '18 at 22:53















0















I'm having trouble finding much information on MongoDB's use of indexes when querying views. The docs state:




Index Use and Sort Operations



Views use the indexes of the underlying collection.



As the indexes are on the underlying collection, you cannot create,
drop or re-build indexes on the view directly nor get a list of
indexes on the view.



You cannot specify a $natural sort on a view.




aaaand that's about it. I'm trying to figure out how to properly index my underlying collections (based on my experience with SQL DBs) to maximize performance of my app's queries on views.



For example, let's say I have a collection holding Product entities in a products collection and which share deleted and categoryId attributes. On top of that I might create a view productsActive defined as deleted: false , and as such on an index on the products collection: deleted: 1 . Now let's say that I commonly do the following query:



db.productsActive.find(categoryId: 'turntable');


Intuitively I want to use a compound index here on the products table:



db.products.createIndex(deleted: 1, categoryId: 1);


Am I doing it right? I suppose ultimately I'm confused by the doc's statement:




Views use the indexes of the underlying collection.




Given that, can mongo use the compound index to initially build the view and then to subsequently query it? Or is simply deriving the view one step (tied to 1 index) and then the subsequent query on the view a second, distinct query with its own index(es)?










share|improve this question






















  • A view is just an "aggregation pipeline", and that is stated quite prominently in the documentation. As such the exact same rules for index usage apply to a view and to an aggregation pipeline. The very first stage of the pipeline used to create the view is the only thing that can use an index. If you have a "question" then simply show how you created the view and any indexes present on the collection it came from, and show your intended usage of said view.

    – Neil Lunn
    Nov 14 '18 at 21:55












  • @NeilLunn "If you have a "question" then simply show how you created the view and any indexes present on the collection it came from, and show your intended usage of said view." That is literally what I included in my question. Did you read it?

    – Madbreaks
    Nov 14 '18 at 22:47












  • Actually you have not done that at all. There is no view definition here or is there any indication of an intended usage pattern for the created view. This is why you were prompted to provide this information. Being Glib and really quite rude typically does not endear people to "help" you. And most people who have something to ask need help. Perhaps you should start reading what is actually being asked of you and make those points clearer.

    – Neil Lunn
    Nov 14 '18 at 22:53













0












0








0








I'm having trouble finding much information on MongoDB's use of indexes when querying views. The docs state:




Index Use and Sort Operations



Views use the indexes of the underlying collection.



As the indexes are on the underlying collection, you cannot create,
drop or re-build indexes on the view directly nor get a list of
indexes on the view.



You cannot specify a $natural sort on a view.




aaaand that's about it. I'm trying to figure out how to properly index my underlying collections (based on my experience with SQL DBs) to maximize performance of my app's queries on views.



For example, let's say I have a collection holding Product entities in a products collection and which share deleted and categoryId attributes. On top of that I might create a view productsActive defined as deleted: false , and as such on an index on the products collection: deleted: 1 . Now let's say that I commonly do the following query:



db.productsActive.find(categoryId: 'turntable');


Intuitively I want to use a compound index here on the products table:



db.products.createIndex(deleted: 1, categoryId: 1);


Am I doing it right? I suppose ultimately I'm confused by the doc's statement:




Views use the indexes of the underlying collection.




Given that, can mongo use the compound index to initially build the view and then to subsequently query it? Or is simply deriving the view one step (tied to 1 index) and then the subsequent query on the view a second, distinct query with its own index(es)?










share|improve this question














I'm having trouble finding much information on MongoDB's use of indexes when querying views. The docs state:




Index Use and Sort Operations



Views use the indexes of the underlying collection.



As the indexes are on the underlying collection, you cannot create,
drop or re-build indexes on the view directly nor get a list of
indexes on the view.



You cannot specify a $natural sort on a view.




aaaand that's about it. I'm trying to figure out how to properly index my underlying collections (based on my experience with SQL DBs) to maximize performance of my app's queries on views.



For example, let's say I have a collection holding Product entities in a products collection and which share deleted and categoryId attributes. On top of that I might create a view productsActive defined as deleted: false , and as such on an index on the products collection: deleted: 1 . Now let's say that I commonly do the following query:



db.productsActive.find(categoryId: 'turntable');


Intuitively I want to use a compound index here on the products table:



db.products.createIndex(deleted: 1, categoryId: 1);


Am I doing it right? I suppose ultimately I'm confused by the doc's statement:




Views use the indexes of the underlying collection.




Given that, can mongo use the compound index to initially build the view and then to subsequently query it? Or is simply deriving the view one step (tied to 1 index) and then the subsequent query on the view a second, distinct query with its own index(es)?







mongodb performance indexing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 21:02









MadbreaksMadbreaks

14.3k43956




14.3k43956












  • A view is just an "aggregation pipeline", and that is stated quite prominently in the documentation. As such the exact same rules for index usage apply to a view and to an aggregation pipeline. The very first stage of the pipeline used to create the view is the only thing that can use an index. If you have a "question" then simply show how you created the view and any indexes present on the collection it came from, and show your intended usage of said view.

    – Neil Lunn
    Nov 14 '18 at 21:55












  • @NeilLunn "If you have a "question" then simply show how you created the view and any indexes present on the collection it came from, and show your intended usage of said view." That is literally what I included in my question. Did you read it?

    – Madbreaks
    Nov 14 '18 at 22:47












  • Actually you have not done that at all. There is no view definition here or is there any indication of an intended usage pattern for the created view. This is why you were prompted to provide this information. Being Glib and really quite rude typically does not endear people to "help" you. And most people who have something to ask need help. Perhaps you should start reading what is actually being asked of you and make those points clearer.

    – Neil Lunn
    Nov 14 '18 at 22:53

















  • A view is just an "aggregation pipeline", and that is stated quite prominently in the documentation. As such the exact same rules for index usage apply to a view and to an aggregation pipeline. The very first stage of the pipeline used to create the view is the only thing that can use an index. If you have a "question" then simply show how you created the view and any indexes present on the collection it came from, and show your intended usage of said view.

    – Neil Lunn
    Nov 14 '18 at 21:55












  • @NeilLunn "If you have a "question" then simply show how you created the view and any indexes present on the collection it came from, and show your intended usage of said view." That is literally what I included in my question. Did you read it?

    – Madbreaks
    Nov 14 '18 at 22:47












  • Actually you have not done that at all. There is no view definition here or is there any indication of an intended usage pattern for the created view. This is why you were prompted to provide this information. Being Glib and really quite rude typically does not endear people to "help" you. And most people who have something to ask need help. Perhaps you should start reading what is actually being asked of you and make those points clearer.

    – Neil Lunn
    Nov 14 '18 at 22:53
















A view is just an "aggregation pipeline", and that is stated quite prominently in the documentation. As such the exact same rules for index usage apply to a view and to an aggregation pipeline. The very first stage of the pipeline used to create the view is the only thing that can use an index. If you have a "question" then simply show how you created the view and any indexes present on the collection it came from, and show your intended usage of said view.

– Neil Lunn
Nov 14 '18 at 21:55






A view is just an "aggregation pipeline", and that is stated quite prominently in the documentation. As such the exact same rules for index usage apply to a view and to an aggregation pipeline. The very first stage of the pipeline used to create the view is the only thing that can use an index. If you have a "question" then simply show how you created the view and any indexes present on the collection it came from, and show your intended usage of said view.

– Neil Lunn
Nov 14 '18 at 21:55














@NeilLunn "If you have a "question" then simply show how you created the view and any indexes present on the collection it came from, and show your intended usage of said view." That is literally what I included in my question. Did you read it?

– Madbreaks
Nov 14 '18 at 22:47






@NeilLunn "If you have a "question" then simply show how you created the view and any indexes present on the collection it came from, and show your intended usage of said view." That is literally what I included in my question. Did you read it?

– Madbreaks
Nov 14 '18 at 22:47














Actually you have not done that at all. There is no view definition here or is there any indication of an intended usage pattern for the created view. This is why you were prompted to provide this information. Being Glib and really quite rude typically does not endear people to "help" you. And most people who have something to ask need help. Perhaps you should start reading what is actually being asked of you and make those points clearer.

– Neil Lunn
Nov 14 '18 at 22:53





Actually you have not done that at all. There is no view definition here or is there any indication of an intended usage pattern for the created view. This is why you were prompted to provide this information. Being Glib and really quite rude typically does not endear people to "help" you. And most people who have something to ask need help. Perhaps you should start reading what is actually being asked of you and make those points clearer.

– Neil Lunn
Nov 14 '18 at 22:53












1 Answer
1






active

oldest

votes


















1














Yes, creating a compound index as described would do the trick. (By the way, you should probably use the background param when building the index.)




Given that, can mongo use the compound index to initially build the
view and then to subsequently query it?




There is really no "building of the view" here, views are created on demand. The index exists on the collection and is used by the view and other queries which match the index definition.



Hope this helps.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308657%2fmongodb-indexes-and-views%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Yes, creating a compound index as described would do the trick. (By the way, you should probably use the background param when building the index.)




    Given that, can mongo use the compound index to initially build the
    view and then to subsequently query it?




    There is really no "building of the view" here, views are created on demand. The index exists on the collection and is used by the view and other queries which match the index definition.



    Hope this helps.






    share|improve this answer



























      1














      Yes, creating a compound index as described would do the trick. (By the way, you should probably use the background param when building the index.)




      Given that, can mongo use the compound index to initially build the
      view and then to subsequently query it?




      There is really no "building of the view" here, views are created on demand. The index exists on the collection and is used by the view and other queries which match the index definition.



      Hope this helps.






      share|improve this answer

























        1












        1








        1







        Yes, creating a compound index as described would do the trick. (By the way, you should probably use the background param when building the index.)




        Given that, can mongo use the compound index to initially build the
        view and then to subsequently query it?




        There is really no "building of the view" here, views are created on demand. The index exists on the collection and is used by the view and other queries which match the index definition.



        Hope this helps.






        share|improve this answer













        Yes, creating a compound index as described would do the trick. (By the way, you should probably use the background param when building the index.)




        Given that, can mongo use the compound index to initially build the
        view and then to subsequently query it?




        There is really no "building of the view" here, views are created on demand. The index exists on the collection and is used by the view and other queries which match the index definition.



        Hope this helps.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 0:42









        mgmonteleonemgmonteleone

        30619




        30619





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308657%2fmongodb-indexes-and-views%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20