MongoDb Query returning unwanted documents











up vote
0
down vote

favorite












I have a database containing documents of two structures:



{
"name": "",
"name_ar": "",
"description": "",
"bla1": {
"name": "",
"link": "",
"Logo": ""
},
"bla2": {
"name": "",
"id": ""
}
}


and



{
"name": "",
"name_ar": "",
"description": "",
"bla1": {
"name": ,
"link": "",
"Logo": ""
},
"bla2": {
"name": "",
"id": ""
}
}


I want to query my collection to get documents with "bla1.name" exactly equal to something. However using the following query:



{$and: [{'bla1.name': {'$type': 'string'}}, {"bla1.name":'something'}]}


returns all documents (even where "bla1.name" is an array) containing the name: 'something'.



What am I doing wrong?










share|improve this question
























  • Why need to check type? Simply db.collection.find({ "bla1.name": "something" }) should work.
    – Anthony Winzlet
    Nov 8 at 10:57










  • @AnthonyWinzlet that does not work as arrays containing the given value get selected, too.
    – mbuechmann
    Nov 8 at 11:02










  • @AnthonyWinzlet that doesnt work if the array contains the name im looking for
    – Charbel Hanna
    Nov 8 at 11:03










  • @mbuechmann Oh yes!!! I had mistaken.
    – Anthony Winzlet
    Nov 8 at 11:11















up vote
0
down vote

favorite












I have a database containing documents of two structures:



{
"name": "",
"name_ar": "",
"description": "",
"bla1": {
"name": "",
"link": "",
"Logo": ""
},
"bla2": {
"name": "",
"id": ""
}
}


and



{
"name": "",
"name_ar": "",
"description": "",
"bla1": {
"name": ,
"link": "",
"Logo": ""
},
"bla2": {
"name": "",
"id": ""
}
}


I want to query my collection to get documents with "bla1.name" exactly equal to something. However using the following query:



{$and: [{'bla1.name': {'$type': 'string'}}, {"bla1.name":'something'}]}


returns all documents (even where "bla1.name" is an array) containing the name: 'something'.



What am I doing wrong?










share|improve this question
























  • Why need to check type? Simply db.collection.find({ "bla1.name": "something" }) should work.
    – Anthony Winzlet
    Nov 8 at 10:57










  • @AnthonyWinzlet that does not work as arrays containing the given value get selected, too.
    – mbuechmann
    Nov 8 at 11:02










  • @AnthonyWinzlet that doesnt work if the array contains the name im looking for
    – Charbel Hanna
    Nov 8 at 11:03










  • @mbuechmann Oh yes!!! I had mistaken.
    – Anthony Winzlet
    Nov 8 at 11:11













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a database containing documents of two structures:



{
"name": "",
"name_ar": "",
"description": "",
"bla1": {
"name": "",
"link": "",
"Logo": ""
},
"bla2": {
"name": "",
"id": ""
}
}


and



{
"name": "",
"name_ar": "",
"description": "",
"bla1": {
"name": ,
"link": "",
"Logo": ""
},
"bla2": {
"name": "",
"id": ""
}
}


I want to query my collection to get documents with "bla1.name" exactly equal to something. However using the following query:



{$and: [{'bla1.name': {'$type': 'string'}}, {"bla1.name":'something'}]}


returns all documents (even where "bla1.name" is an array) containing the name: 'something'.



What am I doing wrong?










share|improve this question















I have a database containing documents of two structures:



{
"name": "",
"name_ar": "",
"description": "",
"bla1": {
"name": "",
"link": "",
"Logo": ""
},
"bla2": {
"name": "",
"id": ""
}
}


and



{
"name": "",
"name_ar": "",
"description": "",
"bla1": {
"name": ,
"link": "",
"Logo": ""
},
"bla2": {
"name": "",
"id": ""
}
}


I want to query my collection to get documents with "bla1.name" exactly equal to something. However using the following query:



{$and: [{'bla1.name': {'$type': 'string'}}, {"bla1.name":'something'}]}


returns all documents (even where "bla1.name" is an array) containing the name: 'something'.



What am I doing wrong?







mongodb mongodb-query






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 11:20









mbuechmann

2,44011123




2,44011123










asked Nov 8 at 10:36









Charbel Hanna

156




156












  • Why need to check type? Simply db.collection.find({ "bla1.name": "something" }) should work.
    – Anthony Winzlet
    Nov 8 at 10:57










  • @AnthonyWinzlet that does not work as arrays containing the given value get selected, too.
    – mbuechmann
    Nov 8 at 11:02










  • @AnthonyWinzlet that doesnt work if the array contains the name im looking for
    – Charbel Hanna
    Nov 8 at 11:03










  • @mbuechmann Oh yes!!! I had mistaken.
    – Anthony Winzlet
    Nov 8 at 11:11


















  • Why need to check type? Simply db.collection.find({ "bla1.name": "something" }) should work.
    – Anthony Winzlet
    Nov 8 at 10:57










  • @AnthonyWinzlet that does not work as arrays containing the given value get selected, too.
    – mbuechmann
    Nov 8 at 11:02










  • @AnthonyWinzlet that doesnt work if the array contains the name im looking for
    – Charbel Hanna
    Nov 8 at 11:03










  • @mbuechmann Oh yes!!! I had mistaken.
    – Anthony Winzlet
    Nov 8 at 11:11
















Why need to check type? Simply db.collection.find({ "bla1.name": "something" }) should work.
– Anthony Winzlet
Nov 8 at 10:57




Why need to check type? Simply db.collection.find({ "bla1.name": "something" }) should work.
– Anthony Winzlet
Nov 8 at 10:57












@AnthonyWinzlet that does not work as arrays containing the given value get selected, too.
– mbuechmann
Nov 8 at 11:02




@AnthonyWinzlet that does not work as arrays containing the given value get selected, too.
– mbuechmann
Nov 8 at 11:02












@AnthonyWinzlet that doesnt work if the array contains the name im looking for
– Charbel Hanna
Nov 8 at 11:03




@AnthonyWinzlet that doesnt work if the array contains the name im looking for
– Charbel Hanna
Nov 8 at 11:03












@mbuechmann Oh yes!!! I had mistaken.
– Anthony Winzlet
Nov 8 at 11:11




@mbuechmann Oh yes!!! I had mistaken.
– Anthony Winzlet
Nov 8 at 11:11












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










From the MongoDB docs:




$type now works with arrays in the same way it works with other BSON types. Previous versions only matched documents where the field contained a nested array.




That means: If an array has at least one element with the given type it gets selected.



If you want to exclude arrays as type you have to extend your query. As the query already matches strings, you can exclude the type selection for string:



$and: [
// not necessary any more, as this selection is already implied by the last part
// {
// "bla1.name": {
// "$type": "string"
// }
// },
{
"bla1.name": {
$not: {
"$type": "array"
}
}
}, {
"bla1.name": "something"
}
]


See the official docs: https://docs.mongodb.com/manual/reference/operator/query/type/#behavior



Here is a working demo on the Mongo playground: https://mongoplayground.net/p/3ri7Bjfrae8






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',
    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%2f53205956%2fmongodb-query-returning-unwanted-documents%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    From the MongoDB docs:




    $type now works with arrays in the same way it works with other BSON types. Previous versions only matched documents where the field contained a nested array.




    That means: If an array has at least one element with the given type it gets selected.



    If you want to exclude arrays as type you have to extend your query. As the query already matches strings, you can exclude the type selection for string:



    $and: [
    // not necessary any more, as this selection is already implied by the last part
    // {
    // "bla1.name": {
    // "$type": "string"
    // }
    // },
    {
    "bla1.name": {
    $not: {
    "$type": "array"
    }
    }
    }, {
    "bla1.name": "something"
    }
    ]


    See the official docs: https://docs.mongodb.com/manual/reference/operator/query/type/#behavior



    Here is a working demo on the Mongo playground: https://mongoplayground.net/p/3ri7Bjfrae8






    share|improve this answer



























      up vote
      2
      down vote



      accepted










      From the MongoDB docs:




      $type now works with arrays in the same way it works with other BSON types. Previous versions only matched documents where the field contained a nested array.




      That means: If an array has at least one element with the given type it gets selected.



      If you want to exclude arrays as type you have to extend your query. As the query already matches strings, you can exclude the type selection for string:



      $and: [
      // not necessary any more, as this selection is already implied by the last part
      // {
      // "bla1.name": {
      // "$type": "string"
      // }
      // },
      {
      "bla1.name": {
      $not: {
      "$type": "array"
      }
      }
      }, {
      "bla1.name": "something"
      }
      ]


      See the official docs: https://docs.mongodb.com/manual/reference/operator/query/type/#behavior



      Here is a working demo on the Mongo playground: https://mongoplayground.net/p/3ri7Bjfrae8






      share|improve this answer

























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        From the MongoDB docs:




        $type now works with arrays in the same way it works with other BSON types. Previous versions only matched documents where the field contained a nested array.




        That means: If an array has at least one element with the given type it gets selected.



        If you want to exclude arrays as type you have to extend your query. As the query already matches strings, you can exclude the type selection for string:



        $and: [
        // not necessary any more, as this selection is already implied by the last part
        // {
        // "bla1.name": {
        // "$type": "string"
        // }
        // },
        {
        "bla1.name": {
        $not: {
        "$type": "array"
        }
        }
        }, {
        "bla1.name": "something"
        }
        ]


        See the official docs: https://docs.mongodb.com/manual/reference/operator/query/type/#behavior



        Here is a working demo on the Mongo playground: https://mongoplayground.net/p/3ri7Bjfrae8






        share|improve this answer














        From the MongoDB docs:




        $type now works with arrays in the same way it works with other BSON types. Previous versions only matched documents where the field contained a nested array.




        That means: If an array has at least one element with the given type it gets selected.



        If you want to exclude arrays as type you have to extend your query. As the query already matches strings, you can exclude the type selection for string:



        $and: [
        // not necessary any more, as this selection is already implied by the last part
        // {
        // "bla1.name": {
        // "$type": "string"
        // }
        // },
        {
        "bla1.name": {
        $not: {
        "$type": "array"
        }
        }
        }, {
        "bla1.name": "something"
        }
        ]


        See the official docs: https://docs.mongodb.com/manual/reference/operator/query/type/#behavior



        Here is a working demo on the Mongo playground: https://mongoplayground.net/p/3ri7Bjfrae8







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 8 at 11:06

























        answered Nov 8 at 10:59









        mbuechmann

        2,44011123




        2,44011123






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205956%2fmongodb-query-returning-unwanted-documents%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Schultheiß

            Liste der Kulturdenkmale in Wilsdruff

            Android Play Services Check