Return object without its key in MongoDb










1















I am using MongoDb 3.2



Say I have this document in a MongoDb collection:




"_id" : ObjectId("5bad65b9777f6df3ce840fd1"),
"entryCode" : "1234",
"first" :
"someKey" : "x",
"anotherKey" : "y"
,
"second" :
"someKey" : "u",
"anotherKey" : "v",




When I execute the following query:
db.collection.find(entryCode:"1234",_id:0, first:1)



I get this returned result:




"first" :
"someKey" : "x",
"anotherKey" : "y"




However, what I'd like to return is this:




"someKey" : "x",
"anotherKey" : "y"



Notice I do not want the key first to be a part of the returned value. Just the object value within. What Mongo query can I use?










share|improve this question




























    1















    I am using MongoDb 3.2



    Say I have this document in a MongoDb collection:




    "_id" : ObjectId("5bad65b9777f6df3ce840fd1"),
    "entryCode" : "1234",
    "first" :
    "someKey" : "x",
    "anotherKey" : "y"
    ,
    "second" :
    "someKey" : "u",
    "anotherKey" : "v",




    When I execute the following query:
    db.collection.find(entryCode:"1234",_id:0, first:1)



    I get this returned result:




    "first" :
    "someKey" : "x",
    "anotherKey" : "y"




    However, what I'd like to return is this:




    "someKey" : "x",
    "anotherKey" : "y"



    Notice I do not want the key first to be a part of the returned value. Just the object value within. What Mongo query can I use?










    share|improve this question


























      1












      1








      1








      I am using MongoDb 3.2



      Say I have this document in a MongoDb collection:




      "_id" : ObjectId("5bad65b9777f6df3ce840fd1"),
      "entryCode" : "1234",
      "first" :
      "someKey" : "x",
      "anotherKey" : "y"
      ,
      "second" :
      "someKey" : "u",
      "anotherKey" : "v",




      When I execute the following query:
      db.collection.find(entryCode:"1234",_id:0, first:1)



      I get this returned result:




      "first" :
      "someKey" : "x",
      "anotherKey" : "y"




      However, what I'd like to return is this:




      "someKey" : "x",
      "anotherKey" : "y"



      Notice I do not want the key first to be a part of the returned value. Just the object value within. What Mongo query can I use?










      share|improve this question
















      I am using MongoDb 3.2



      Say I have this document in a MongoDb collection:




      "_id" : ObjectId("5bad65b9777f6df3ce840fd1"),
      "entryCode" : "1234",
      "first" :
      "someKey" : "x",
      "anotherKey" : "y"
      ,
      "second" :
      "someKey" : "u",
      "anotherKey" : "v",




      When I execute the following query:
      db.collection.find(entryCode:"1234",_id:0, first:1)



      I get this returned result:




      "first" :
      "someKey" : "x",
      "anotherKey" : "y"




      However, what I'd like to return is this:




      "someKey" : "x",
      "anotherKey" : "y"



      Notice I do not want the key first to be a part of the returned value. Just the object value within. What Mongo query can I use?







      mongodb mongodb-query






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 21:33







      Cody Patterson

















      asked Nov 13 '18 at 21:21









      Cody PattersonCody Patterson

      747




      747






















          1 Answer
          1






          active

          oldest

          votes


















          3














          You need $replaceRoot operator, try:



          db.col.aggregate([
          $match: entryCode:"1234" ,

          $replaceRoot:
          newRoot: "$first"


          ])


          EDIT: In MongoDB 3.2 you can only use $project and specify fields explicitly:



          db.col.aggregate([
          $match: entryCode:"1234" ,

          $project:
          _id: 0,
          someKey: "$first.someKey",
          anotherKey: "$first.anotherKey",


          ])





          share|improve this answer

























          • I like your answer, but unfortunately I'm using 3.2 and $replaceRoot is only available in 3.4. I've edited my question to clarify.

            – Cody Patterson
            Nov 13 '18 at 21:34











          • @CodyPatterson okay so then you have to use $project, modified my answer

            – mickl
            Nov 13 '18 at 21:37











          • Thank you. This solved the issue.

            – Cody Patterson
            Nov 13 '18 at 21:44










          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%2f53289666%2freturn-object-without-its-key-in-mongodb%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









          3














          You need $replaceRoot operator, try:



          db.col.aggregate([
          $match: entryCode:"1234" ,

          $replaceRoot:
          newRoot: "$first"


          ])


          EDIT: In MongoDB 3.2 you can only use $project and specify fields explicitly:



          db.col.aggregate([
          $match: entryCode:"1234" ,

          $project:
          _id: 0,
          someKey: "$first.someKey",
          anotherKey: "$first.anotherKey",


          ])





          share|improve this answer

























          • I like your answer, but unfortunately I'm using 3.2 and $replaceRoot is only available in 3.4. I've edited my question to clarify.

            – Cody Patterson
            Nov 13 '18 at 21:34











          • @CodyPatterson okay so then you have to use $project, modified my answer

            – mickl
            Nov 13 '18 at 21:37











          • Thank you. This solved the issue.

            – Cody Patterson
            Nov 13 '18 at 21:44















          3














          You need $replaceRoot operator, try:



          db.col.aggregate([
          $match: entryCode:"1234" ,

          $replaceRoot:
          newRoot: "$first"


          ])


          EDIT: In MongoDB 3.2 you can only use $project and specify fields explicitly:



          db.col.aggregate([
          $match: entryCode:"1234" ,

          $project:
          _id: 0,
          someKey: "$first.someKey",
          anotherKey: "$first.anotherKey",


          ])





          share|improve this answer

























          • I like your answer, but unfortunately I'm using 3.2 and $replaceRoot is only available in 3.4. I've edited my question to clarify.

            – Cody Patterson
            Nov 13 '18 at 21:34











          • @CodyPatterson okay so then you have to use $project, modified my answer

            – mickl
            Nov 13 '18 at 21:37











          • Thank you. This solved the issue.

            – Cody Patterson
            Nov 13 '18 at 21:44













          3












          3








          3







          You need $replaceRoot operator, try:



          db.col.aggregate([
          $match: entryCode:"1234" ,

          $replaceRoot:
          newRoot: "$first"


          ])


          EDIT: In MongoDB 3.2 you can only use $project and specify fields explicitly:



          db.col.aggregate([
          $match: entryCode:"1234" ,

          $project:
          _id: 0,
          someKey: "$first.someKey",
          anotherKey: "$first.anotherKey",


          ])





          share|improve this answer















          You need $replaceRoot operator, try:



          db.col.aggregate([
          $match: entryCode:"1234" ,

          $replaceRoot:
          newRoot: "$first"


          ])


          EDIT: In MongoDB 3.2 you can only use $project and specify fields explicitly:



          db.col.aggregate([
          $match: entryCode:"1234" ,

          $project:
          _id: 0,
          someKey: "$first.someKey",
          anotherKey: "$first.anotherKey",


          ])






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 21:34

























          answered Nov 13 '18 at 21:22









          micklmickl

          14k51536




          14k51536












          • I like your answer, but unfortunately I'm using 3.2 and $replaceRoot is only available in 3.4. I've edited my question to clarify.

            – Cody Patterson
            Nov 13 '18 at 21:34











          • @CodyPatterson okay so then you have to use $project, modified my answer

            – mickl
            Nov 13 '18 at 21:37











          • Thank you. This solved the issue.

            – Cody Patterson
            Nov 13 '18 at 21:44

















          • I like your answer, but unfortunately I'm using 3.2 and $replaceRoot is only available in 3.4. I've edited my question to clarify.

            – Cody Patterson
            Nov 13 '18 at 21:34











          • @CodyPatterson okay so then you have to use $project, modified my answer

            – mickl
            Nov 13 '18 at 21:37











          • Thank you. This solved the issue.

            – Cody Patterson
            Nov 13 '18 at 21:44
















          I like your answer, but unfortunately I'm using 3.2 and $replaceRoot is only available in 3.4. I've edited my question to clarify.

          – Cody Patterson
          Nov 13 '18 at 21:34





          I like your answer, but unfortunately I'm using 3.2 and $replaceRoot is only available in 3.4. I've edited my question to clarify.

          – Cody Patterson
          Nov 13 '18 at 21:34













          @CodyPatterson okay so then you have to use $project, modified my answer

          – mickl
          Nov 13 '18 at 21:37





          @CodyPatterson okay so then you have to use $project, modified my answer

          – mickl
          Nov 13 '18 at 21:37













          Thank you. This solved the issue.

          – Cody Patterson
          Nov 13 '18 at 21:44





          Thank you. This solved the issue.

          – Cody Patterson
          Nov 13 '18 at 21:44



















          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%2f53289666%2freturn-object-without-its-key-in-mongodb%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

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo