mongodb query and return specific collections












1















I'm trying to return all collections where field grade: 'Grade Two', I've tried a number of queries but getting either error or nothing being returned. I want my query only return documents from the collection with 'Grade Two', that should return only two docs.



I've tried the below queries:



db.users.find({grade: "Grade Two"})
db.users.find({}, {grade: "Grade Two"})
db.users.find({}, {profile: {grade: "Grade Two"}})


I'm getting error with message: "errmsg" : "Unsupported projection option: profile: { grade: "Grade Two" }",



{
{
"_id": "4YH8hDjNhN39CTtZh",
"username": "philcee.philips",
"emails": [
{
"address": "ph@gmail.com",
"verified": false
}
],
"profile": {
"name": {
"firstname": "Philcee",
"lastname": "Philips"
},
"gender": "Female",
"nationality": "foo",
"grade": "Grade Two"
},
"roles": {
"__global_roles__": [
"student"
]
},
{
"_id": "8UH8hDjNhN39CTtm6",
"username": "gibson.wilson",
"emails": [
{
"address": "wil@gmail.com",
"verified": false
}
],
"profile": {
"name": {
"firstname": "Gibson",
"lastname": "Wilson"
},
"gender": "Male",
"nationality": "bar",
"grade": "Grade Two"
},
"roles": {
"__global_roles__": [
"student"
]
},


{
"_id": "i7G8hDjKhN39CTYt9",
"username": "daniel.jones",
"emails": [
{
"address": "jones@gmail.com",
"verified": false
}
],
"profile": {
"name": {
"firstname": "Daniel",
"lastname": "Jones"
},
"gender": "Male",
"nationality": "bar",
"grade": "Grade One"
},
"roles": {
"__global_roles__": [
"student"
]
}
}


Is there another way to query for specific docs from the collection mentioned?










share|improve this question

























  • Use .dot notation db.users.find({ "profile.grade": "Grade Two" }) and for projection db.users.find({ "profile.grade": "Grade Two" }, { "profile.grade": 1 })

    – Anthony Winzlet
    Nov 25 '18 at 15:50













  • Great, thanks. This was really helpful. All good now.

    – Israel Z Kollie
    Nov 25 '18 at 16:22
















1















I'm trying to return all collections where field grade: 'Grade Two', I've tried a number of queries but getting either error or nothing being returned. I want my query only return documents from the collection with 'Grade Two', that should return only two docs.



I've tried the below queries:



db.users.find({grade: "Grade Two"})
db.users.find({}, {grade: "Grade Two"})
db.users.find({}, {profile: {grade: "Grade Two"}})


I'm getting error with message: "errmsg" : "Unsupported projection option: profile: { grade: "Grade Two" }",



{
{
"_id": "4YH8hDjNhN39CTtZh",
"username": "philcee.philips",
"emails": [
{
"address": "ph@gmail.com",
"verified": false
}
],
"profile": {
"name": {
"firstname": "Philcee",
"lastname": "Philips"
},
"gender": "Female",
"nationality": "foo",
"grade": "Grade Two"
},
"roles": {
"__global_roles__": [
"student"
]
},
{
"_id": "8UH8hDjNhN39CTtm6",
"username": "gibson.wilson",
"emails": [
{
"address": "wil@gmail.com",
"verified": false
}
],
"profile": {
"name": {
"firstname": "Gibson",
"lastname": "Wilson"
},
"gender": "Male",
"nationality": "bar",
"grade": "Grade Two"
},
"roles": {
"__global_roles__": [
"student"
]
},


{
"_id": "i7G8hDjKhN39CTYt9",
"username": "daniel.jones",
"emails": [
{
"address": "jones@gmail.com",
"verified": false
}
],
"profile": {
"name": {
"firstname": "Daniel",
"lastname": "Jones"
},
"gender": "Male",
"nationality": "bar",
"grade": "Grade One"
},
"roles": {
"__global_roles__": [
"student"
]
}
}


Is there another way to query for specific docs from the collection mentioned?










share|improve this question

























  • Use .dot notation db.users.find({ "profile.grade": "Grade Two" }) and for projection db.users.find({ "profile.grade": "Grade Two" }, { "profile.grade": 1 })

    – Anthony Winzlet
    Nov 25 '18 at 15:50













  • Great, thanks. This was really helpful. All good now.

    – Israel Z Kollie
    Nov 25 '18 at 16:22














1












1








1








I'm trying to return all collections where field grade: 'Grade Two', I've tried a number of queries but getting either error or nothing being returned. I want my query only return documents from the collection with 'Grade Two', that should return only two docs.



I've tried the below queries:



db.users.find({grade: "Grade Two"})
db.users.find({}, {grade: "Grade Two"})
db.users.find({}, {profile: {grade: "Grade Two"}})


I'm getting error with message: "errmsg" : "Unsupported projection option: profile: { grade: "Grade Two" }",



{
{
"_id": "4YH8hDjNhN39CTtZh",
"username": "philcee.philips",
"emails": [
{
"address": "ph@gmail.com",
"verified": false
}
],
"profile": {
"name": {
"firstname": "Philcee",
"lastname": "Philips"
},
"gender": "Female",
"nationality": "foo",
"grade": "Grade Two"
},
"roles": {
"__global_roles__": [
"student"
]
},
{
"_id": "8UH8hDjNhN39CTtm6",
"username": "gibson.wilson",
"emails": [
{
"address": "wil@gmail.com",
"verified": false
}
],
"profile": {
"name": {
"firstname": "Gibson",
"lastname": "Wilson"
},
"gender": "Male",
"nationality": "bar",
"grade": "Grade Two"
},
"roles": {
"__global_roles__": [
"student"
]
},


{
"_id": "i7G8hDjKhN39CTYt9",
"username": "daniel.jones",
"emails": [
{
"address": "jones@gmail.com",
"verified": false
}
],
"profile": {
"name": {
"firstname": "Daniel",
"lastname": "Jones"
},
"gender": "Male",
"nationality": "bar",
"grade": "Grade One"
},
"roles": {
"__global_roles__": [
"student"
]
}
}


Is there another way to query for specific docs from the collection mentioned?










share|improve this question
















I'm trying to return all collections where field grade: 'Grade Two', I've tried a number of queries but getting either error or nothing being returned. I want my query only return documents from the collection with 'Grade Two', that should return only two docs.



I've tried the below queries:



db.users.find({grade: "Grade Two"})
db.users.find({}, {grade: "Grade Two"})
db.users.find({}, {profile: {grade: "Grade Two"}})


I'm getting error with message: "errmsg" : "Unsupported projection option: profile: { grade: "Grade Two" }",



{
{
"_id": "4YH8hDjNhN39CTtZh",
"username": "philcee.philips",
"emails": [
{
"address": "ph@gmail.com",
"verified": false
}
],
"profile": {
"name": {
"firstname": "Philcee",
"lastname": "Philips"
},
"gender": "Female",
"nationality": "foo",
"grade": "Grade Two"
},
"roles": {
"__global_roles__": [
"student"
]
},
{
"_id": "8UH8hDjNhN39CTtm6",
"username": "gibson.wilson",
"emails": [
{
"address": "wil@gmail.com",
"verified": false
}
],
"profile": {
"name": {
"firstname": "Gibson",
"lastname": "Wilson"
},
"gender": "Male",
"nationality": "bar",
"grade": "Grade Two"
},
"roles": {
"__global_roles__": [
"student"
]
},


{
"_id": "i7G8hDjKhN39CTYt9",
"username": "daniel.jones",
"emails": [
{
"address": "jones@gmail.com",
"verified": false
}
],
"profile": {
"name": {
"firstname": "Daniel",
"lastname": "Jones"
},
"gender": "Male",
"nationality": "bar",
"grade": "Grade One"
},
"roles": {
"__global_roles__": [
"student"
]
}
}


Is there another way to query for specific docs from the collection mentioned?







mongodb mongodb-query






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 16:24









Anthony Winzlet

15.4k41339




15.4k41339










asked Nov 25 '18 at 15:15









Israel Z KollieIsrael Z Kollie

809




809













  • Use .dot notation db.users.find({ "profile.grade": "Grade Two" }) and for projection db.users.find({ "profile.grade": "Grade Two" }, { "profile.grade": 1 })

    – Anthony Winzlet
    Nov 25 '18 at 15:50













  • Great, thanks. This was really helpful. All good now.

    – Israel Z Kollie
    Nov 25 '18 at 16:22



















  • Use .dot notation db.users.find({ "profile.grade": "Grade Two" }) and for projection db.users.find({ "profile.grade": "Grade Two" }, { "profile.grade": 1 })

    – Anthony Winzlet
    Nov 25 '18 at 15:50













  • Great, thanks. This was really helpful. All good now.

    – Israel Z Kollie
    Nov 25 '18 at 16:22

















Use .dot notation db.users.find({ "profile.grade": "Grade Two" }) and for projection db.users.find({ "profile.grade": "Grade Two" }, { "profile.grade": 1 })

– Anthony Winzlet
Nov 25 '18 at 15:50







Use .dot notation db.users.find({ "profile.grade": "Grade Two" }) and for projection db.users.find({ "profile.grade": "Grade Two" }, { "profile.grade": 1 })

– Anthony Winzlet
Nov 25 '18 at 15:50















Great, thanks. This was really helpful. All good now.

– Israel Z Kollie
Nov 25 '18 at 16:22





Great, thanks. This was really helpful. All good now.

– Israel Z Kollie
Nov 25 '18 at 16:22












1 Answer
1






active

oldest

votes


















2














You can use .dot notation to find inside an object



db.users.find({ "profile.grade": "Grade Two" }) 


And to return specific field you can use projection in the second argument of the find query



db.users.find({ "profile.grade": "Grade Two" }, { "profile.grade": 1 })





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%2f53468888%2fmongodb-query-and-return-specific-collections%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









    2














    You can use .dot notation to find inside an object



    db.users.find({ "profile.grade": "Grade Two" }) 


    And to return specific field you can use projection in the second argument of the find query



    db.users.find({ "profile.grade": "Grade Two" }, { "profile.grade": 1 })





    share|improve this answer




























      2














      You can use .dot notation to find inside an object



      db.users.find({ "profile.grade": "Grade Two" }) 


      And to return specific field you can use projection in the second argument of the find query



      db.users.find({ "profile.grade": "Grade Two" }, { "profile.grade": 1 })





      share|improve this answer


























        2












        2








        2







        You can use .dot notation to find inside an object



        db.users.find({ "profile.grade": "Grade Two" }) 


        And to return specific field you can use projection in the second argument of the find query



        db.users.find({ "profile.grade": "Grade Two" }, { "profile.grade": 1 })





        share|improve this answer













        You can use .dot notation to find inside an object



        db.users.find({ "profile.grade": "Grade Two" }) 


        And to return specific field you can use projection in the second argument of the find query



        db.users.find({ "profile.grade": "Grade Two" }, { "profile.grade": 1 })






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 16:28









        Anthony WinzletAnthony Winzlet

        15.4k41339




        15.4k41339






























            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%2f53468888%2fmongodb-query-and-return-specific-collections%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

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)