Why do aggregating a filtered dataset lose the filters of it?












0















I have this collection:



// collection
[
{_id: 1, name: 'Luigi', childs: [{name: 'one'}, {name: 'two'}], dad_id: 9]},
{_id: 1, name: 'Mario', childs: [{name: 'four'}, {name: 'five'}], dad_id: 8]},
{_id: 1, name: 'Alessandro', childs: [{name: 'seven'}, {name: 'six'}], dad_id: 9]},
]


and apply this filter to it



result = collection.find({ dad_id: 9 })


Then I want to aggregate the results and get all the childs singularly, I start with unwinding them
(then I 'll make a projection, etc..) but I already encounter a behavior that I do not understand:
the result contains also the documents with dad_id is 8, even if they were already excluded by my query.



result.aggregate([
{ "$unwind"=> "$childs" },
]).each do |e| ... end
// => [
{_id: 1, name: 'Luigi', childs: {name: 'one'}, dad_id: 9]},
{_id: 1, name: 'Luigi', childs: {name: 'two'}, dad_id: 9]},
{_id: 1, name: 'Luigi', childs: {name: 'five'}, dad_id: 8]},
{_id: 1, name: 'Luigi', childs: {name: 'four'}, dad_id: 8]},
{_id: 1, name: 'Luigi', childs: {name: 'seven'}, dad_id: 9]},
{_id: 1, name: 'Luigi', childs: {name: 'six'}, dad_id: 9]},
]


What am I missing?










share|improve this question

























  • Which driver is this? You should not even be able to do that. It's certainly not standard with any other language driver and since it is not working, then this "feature" is not even implemented properly. You're supposed to use a $match stage in the aggregation pipeline. And MongoDB "views" are something completely different.

    – Neil Lunn
    Nov 22 '18 at 10:54











  • Yeah about the view your are right, I am calling them poorly. I will edit the question. The driver any way is ruby. My question remain the same but change the word view with "dataset"

    – ciaoben
    Nov 22 '18 at 10:56











  • It's not a matter of what you are calling it, but rather that no officially supported driver has any such feature as you are claiming. So what you need to tell us is "where did you install this driver from?"

    – Neil Lunn
    Nov 22 '18 at 10:58











  • @NeilLunn it is the official gem "mongo" version 2.3.0

    – ciaoben
    Nov 22 '18 at 11:01













  • Yeah was just looking at it and it's pretty bad that this is exposed this way. As stated, you are not actually meant to even try and use it that way, and no other language driver even allows it. Instead you $match in the pipeline itself collection.aggregate([{ "$match" => { dad_id: 9 } },{ "$unwind"=> "$childs" }]). So the "bugginess" in the driver is the find() returns a "Collection View" which exposes an aggregate() method which should not actually be there. Do it with the Collection method like everyone else does.

    – Neil Lunn
    Nov 22 '18 at 11:12
















0















I have this collection:



// collection
[
{_id: 1, name: 'Luigi', childs: [{name: 'one'}, {name: 'two'}], dad_id: 9]},
{_id: 1, name: 'Mario', childs: [{name: 'four'}, {name: 'five'}], dad_id: 8]},
{_id: 1, name: 'Alessandro', childs: [{name: 'seven'}, {name: 'six'}], dad_id: 9]},
]


and apply this filter to it



result = collection.find({ dad_id: 9 })


Then I want to aggregate the results and get all the childs singularly, I start with unwinding them
(then I 'll make a projection, etc..) but I already encounter a behavior that I do not understand:
the result contains also the documents with dad_id is 8, even if they were already excluded by my query.



result.aggregate([
{ "$unwind"=> "$childs" },
]).each do |e| ... end
// => [
{_id: 1, name: 'Luigi', childs: {name: 'one'}, dad_id: 9]},
{_id: 1, name: 'Luigi', childs: {name: 'two'}, dad_id: 9]},
{_id: 1, name: 'Luigi', childs: {name: 'five'}, dad_id: 8]},
{_id: 1, name: 'Luigi', childs: {name: 'four'}, dad_id: 8]},
{_id: 1, name: 'Luigi', childs: {name: 'seven'}, dad_id: 9]},
{_id: 1, name: 'Luigi', childs: {name: 'six'}, dad_id: 9]},
]


What am I missing?










share|improve this question

























  • Which driver is this? You should not even be able to do that. It's certainly not standard with any other language driver and since it is not working, then this "feature" is not even implemented properly. You're supposed to use a $match stage in the aggregation pipeline. And MongoDB "views" are something completely different.

    – Neil Lunn
    Nov 22 '18 at 10:54











  • Yeah about the view your are right, I am calling them poorly. I will edit the question. The driver any way is ruby. My question remain the same but change the word view with "dataset"

    – ciaoben
    Nov 22 '18 at 10:56











  • It's not a matter of what you are calling it, but rather that no officially supported driver has any such feature as you are claiming. So what you need to tell us is "where did you install this driver from?"

    – Neil Lunn
    Nov 22 '18 at 10:58











  • @NeilLunn it is the official gem "mongo" version 2.3.0

    – ciaoben
    Nov 22 '18 at 11:01













  • Yeah was just looking at it and it's pretty bad that this is exposed this way. As stated, you are not actually meant to even try and use it that way, and no other language driver even allows it. Instead you $match in the pipeline itself collection.aggregate([{ "$match" => { dad_id: 9 } },{ "$unwind"=> "$childs" }]). So the "bugginess" in the driver is the find() returns a "Collection View" which exposes an aggregate() method which should not actually be there. Do it with the Collection method like everyone else does.

    – Neil Lunn
    Nov 22 '18 at 11:12














0












0








0








I have this collection:



// collection
[
{_id: 1, name: 'Luigi', childs: [{name: 'one'}, {name: 'two'}], dad_id: 9]},
{_id: 1, name: 'Mario', childs: [{name: 'four'}, {name: 'five'}], dad_id: 8]},
{_id: 1, name: 'Alessandro', childs: [{name: 'seven'}, {name: 'six'}], dad_id: 9]},
]


and apply this filter to it



result = collection.find({ dad_id: 9 })


Then I want to aggregate the results and get all the childs singularly, I start with unwinding them
(then I 'll make a projection, etc..) but I already encounter a behavior that I do not understand:
the result contains also the documents with dad_id is 8, even if they were already excluded by my query.



result.aggregate([
{ "$unwind"=> "$childs" },
]).each do |e| ... end
// => [
{_id: 1, name: 'Luigi', childs: {name: 'one'}, dad_id: 9]},
{_id: 1, name: 'Luigi', childs: {name: 'two'}, dad_id: 9]},
{_id: 1, name: 'Luigi', childs: {name: 'five'}, dad_id: 8]},
{_id: 1, name: 'Luigi', childs: {name: 'four'}, dad_id: 8]},
{_id: 1, name: 'Luigi', childs: {name: 'seven'}, dad_id: 9]},
{_id: 1, name: 'Luigi', childs: {name: 'six'}, dad_id: 9]},
]


What am I missing?










share|improve this question
















I have this collection:



// collection
[
{_id: 1, name: 'Luigi', childs: [{name: 'one'}, {name: 'two'}], dad_id: 9]},
{_id: 1, name: 'Mario', childs: [{name: 'four'}, {name: 'five'}], dad_id: 8]},
{_id: 1, name: 'Alessandro', childs: [{name: 'seven'}, {name: 'six'}], dad_id: 9]},
]


and apply this filter to it



result = collection.find({ dad_id: 9 })


Then I want to aggregate the results and get all the childs singularly, I start with unwinding them
(then I 'll make a projection, etc..) but I already encounter a behavior that I do not understand:
the result contains also the documents with dad_id is 8, even if they were already excluded by my query.



result.aggregate([
{ "$unwind"=> "$childs" },
]).each do |e| ... end
// => [
{_id: 1, name: 'Luigi', childs: {name: 'one'}, dad_id: 9]},
{_id: 1, name: 'Luigi', childs: {name: 'two'}, dad_id: 9]},
{_id: 1, name: 'Luigi', childs: {name: 'five'}, dad_id: 8]},
{_id: 1, name: 'Luigi', childs: {name: 'four'}, dad_id: 8]},
{_id: 1, name: 'Luigi', childs: {name: 'seven'}, dad_id: 9]},
{_id: 1, name: 'Luigi', childs: {name: 'six'}, dad_id: 9]},
]


What am I missing?







ruby mongodb






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 10:57







ciaoben

















asked Nov 22 '18 at 10:47









ciaobenciaoben

794920




794920













  • Which driver is this? You should not even be able to do that. It's certainly not standard with any other language driver and since it is not working, then this "feature" is not even implemented properly. You're supposed to use a $match stage in the aggregation pipeline. And MongoDB "views" are something completely different.

    – Neil Lunn
    Nov 22 '18 at 10:54











  • Yeah about the view your are right, I am calling them poorly. I will edit the question. The driver any way is ruby. My question remain the same but change the word view with "dataset"

    – ciaoben
    Nov 22 '18 at 10:56











  • It's not a matter of what you are calling it, but rather that no officially supported driver has any such feature as you are claiming. So what you need to tell us is "where did you install this driver from?"

    – Neil Lunn
    Nov 22 '18 at 10:58











  • @NeilLunn it is the official gem "mongo" version 2.3.0

    – ciaoben
    Nov 22 '18 at 11:01













  • Yeah was just looking at it and it's pretty bad that this is exposed this way. As stated, you are not actually meant to even try and use it that way, and no other language driver even allows it. Instead you $match in the pipeline itself collection.aggregate([{ "$match" => { dad_id: 9 } },{ "$unwind"=> "$childs" }]). So the "bugginess" in the driver is the find() returns a "Collection View" which exposes an aggregate() method which should not actually be there. Do it with the Collection method like everyone else does.

    – Neil Lunn
    Nov 22 '18 at 11:12



















  • Which driver is this? You should not even be able to do that. It's certainly not standard with any other language driver and since it is not working, then this "feature" is not even implemented properly. You're supposed to use a $match stage in the aggregation pipeline. And MongoDB "views" are something completely different.

    – Neil Lunn
    Nov 22 '18 at 10:54











  • Yeah about the view your are right, I am calling them poorly. I will edit the question. The driver any way is ruby. My question remain the same but change the word view with "dataset"

    – ciaoben
    Nov 22 '18 at 10:56











  • It's not a matter of what you are calling it, but rather that no officially supported driver has any such feature as you are claiming. So what you need to tell us is "where did you install this driver from?"

    – Neil Lunn
    Nov 22 '18 at 10:58











  • @NeilLunn it is the official gem "mongo" version 2.3.0

    – ciaoben
    Nov 22 '18 at 11:01













  • Yeah was just looking at it and it's pretty bad that this is exposed this way. As stated, you are not actually meant to even try and use it that way, and no other language driver even allows it. Instead you $match in the pipeline itself collection.aggregate([{ "$match" => { dad_id: 9 } },{ "$unwind"=> "$childs" }]). So the "bugginess" in the driver is the find() returns a "Collection View" which exposes an aggregate() method which should not actually be there. Do it with the Collection method like everyone else does.

    – Neil Lunn
    Nov 22 '18 at 11:12

















Which driver is this? You should not even be able to do that. It's certainly not standard with any other language driver and since it is not working, then this "feature" is not even implemented properly. You're supposed to use a $match stage in the aggregation pipeline. And MongoDB "views" are something completely different.

– Neil Lunn
Nov 22 '18 at 10:54





Which driver is this? You should not even be able to do that. It's certainly not standard with any other language driver and since it is not working, then this "feature" is not even implemented properly. You're supposed to use a $match stage in the aggregation pipeline. And MongoDB "views" are something completely different.

– Neil Lunn
Nov 22 '18 at 10:54













Yeah about the view your are right, I am calling them poorly. I will edit the question. The driver any way is ruby. My question remain the same but change the word view with "dataset"

– ciaoben
Nov 22 '18 at 10:56





Yeah about the view your are right, I am calling them poorly. I will edit the question. The driver any way is ruby. My question remain the same but change the word view with "dataset"

– ciaoben
Nov 22 '18 at 10:56













It's not a matter of what you are calling it, but rather that no officially supported driver has any such feature as you are claiming. So what you need to tell us is "where did you install this driver from?"

– Neil Lunn
Nov 22 '18 at 10:58





It's not a matter of what you are calling it, but rather that no officially supported driver has any such feature as you are claiming. So what you need to tell us is "where did you install this driver from?"

– Neil Lunn
Nov 22 '18 at 10:58













@NeilLunn it is the official gem "mongo" version 2.3.0

– ciaoben
Nov 22 '18 at 11:01







@NeilLunn it is the official gem "mongo" version 2.3.0

– ciaoben
Nov 22 '18 at 11:01















Yeah was just looking at it and it's pretty bad that this is exposed this way. As stated, you are not actually meant to even try and use it that way, and no other language driver even allows it. Instead you $match in the pipeline itself collection.aggregate([{ "$match" => { dad_id: 9 } },{ "$unwind"=> "$childs" }]). So the "bugginess" in the driver is the find() returns a "Collection View" which exposes an aggregate() method which should not actually be there. Do it with the Collection method like everyone else does.

– Neil Lunn
Nov 22 '18 at 11:12





Yeah was just looking at it and it's pretty bad that this is exposed this way. As stated, you are not actually meant to even try and use it that way, and no other language driver even allows it. Instead you $match in the pipeline itself collection.aggregate([{ "$match" => { dad_id: 9 } },{ "$unwind"=> "$childs" }]). So the "bugginess" in the driver is the find() returns a "Collection View" which exposes an aggregate() method which should not actually be there. Do it with the Collection method like everyone else does.

– Neil Lunn
Nov 22 '18 at 11:12












1 Answer
1






active

oldest

votes


















0














You can not chain input from one query to another query like that.



Either use search query ex. Model.find(id) or aggregation framework.



Aggregation framework provides you the functionality to create a pipeline (ex. match,unwind,lookup,project).



To utilize mongodb indexing always try to use "$match" first in the pipeline



match = { "$match" => { "dad_id" =>9} }
unwind = {"$uwind"=>"$childs"}
pipeline = [match,unwind]
collection.aggregate(pipeline).each do |obj|
end





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%2f53429227%2fwhy-do-aggregating-a-filtered-dataset-lose-the-filters-of-it%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









    0














    You can not chain input from one query to another query like that.



    Either use search query ex. Model.find(id) or aggregation framework.



    Aggregation framework provides you the functionality to create a pipeline (ex. match,unwind,lookup,project).



    To utilize mongodb indexing always try to use "$match" first in the pipeline



    match = { "$match" => { "dad_id" =>9} }
    unwind = {"$uwind"=>"$childs"}
    pipeline = [match,unwind]
    collection.aggregate(pipeline).each do |obj|
    end





    share|improve this answer




























      0














      You can not chain input from one query to another query like that.



      Either use search query ex. Model.find(id) or aggregation framework.



      Aggregation framework provides you the functionality to create a pipeline (ex. match,unwind,lookup,project).



      To utilize mongodb indexing always try to use "$match" first in the pipeline



      match = { "$match" => { "dad_id" =>9} }
      unwind = {"$uwind"=>"$childs"}
      pipeline = [match,unwind]
      collection.aggregate(pipeline).each do |obj|
      end





      share|improve this answer


























        0












        0








        0







        You can not chain input from one query to another query like that.



        Either use search query ex. Model.find(id) or aggregation framework.



        Aggregation framework provides you the functionality to create a pipeline (ex. match,unwind,lookup,project).



        To utilize mongodb indexing always try to use "$match" first in the pipeline



        match = { "$match" => { "dad_id" =>9} }
        unwind = {"$uwind"=>"$childs"}
        pipeline = [match,unwind]
        collection.aggregate(pipeline).each do |obj|
        end





        share|improve this answer













        You can not chain input from one query to another query like that.



        Either use search query ex. Model.find(id) or aggregation framework.



        Aggregation framework provides you the functionality to create a pipeline (ex. match,unwind,lookup,project).



        To utilize mongodb indexing always try to use "$match" first in the pipeline



        match = { "$match" => { "dad_id" =>9} }
        unwind = {"$uwind"=>"$childs"}
        pipeline = [match,unwind]
        collection.aggregate(pipeline).each do |obj|
        end






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 11:59









        Mukul DevMukul Dev

        814




        814
































            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%2f53429227%2fwhy-do-aggregating-a-filtered-dataset-lose-the-filters-of-it%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

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            RAC Tourist Trophy