Get distinct records values












17















Is there a way to query for objects with not same values on some field? For example I have Records:



{ id : 1, name : "my_name", salary : 1200 }
{ id : 2, name : "my_name", salary : 800 }
{ id : 3, name : "john", salary : 500 }


Query : find all with NOT_THE_SAME(name)



I just want records with id 1 and 3 because I specified that I don't want records with same value in field name or 2 and 3, it does not matter in this situation.










share|improve this question





























    17















    Is there a way to query for objects with not same values on some field? For example I have Records:



    { id : 1, name : "my_name", salary : 1200 }
    { id : 2, name : "my_name", salary : 800 }
    { id : 3, name : "john", salary : 500 }


    Query : find all with NOT_THE_SAME(name)



    I just want records with id 1 and 3 because I specified that I don't want records with same value in field name or 2 and 3, it does not matter in this situation.










    share|improve this question



























      17












      17








      17


      3






      Is there a way to query for objects with not same values on some field? For example I have Records:



      { id : 1, name : "my_name", salary : 1200 }
      { id : 2, name : "my_name", salary : 800 }
      { id : 3, name : "john", salary : 500 }


      Query : find all with NOT_THE_SAME(name)



      I just want records with id 1 and 3 because I specified that I don't want records with same value in field name or 2 and 3, it does not matter in this situation.










      share|improve this question
















      Is there a way to query for objects with not same values on some field? For example I have Records:



      { id : 1, name : "my_name", salary : 1200 }
      { id : 2, name : "my_name", salary : 800 }
      { id : 3, name : "john", salary : 500 }


      Query : find all with NOT_THE_SAME(name)



      I just want records with id 1 and 3 because I specified that I don't want records with same value in field name or 2 and 3, it does not matter in this situation.







      mongodb






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 18 '13 at 10:32









      Chris Seymour

      62.9k20125162




      62.9k20125162










      asked Dec 18 '13 at 10:24









      GelidusGelidus

      5451719




      5451719
























          5 Answers
          5






          active

          oldest

          votes


















          14














          You can use db.collection.distinct to get back an array of unique values:



          > db.test.distinct("name")
          [ "my_name", "john" ]





          share|improve this answer































            8














            You can also use a distinct sentence with filtered collection. For example, you can get distinct values of names from salaries over 800 with the following query:



            db.test.distinct("name", { "salary": { $gt: 800 } })





            share|improve this answer































              4














              db.test.aggregate([{$group: {_id: "$name", salary: {$max: "$salary"}}}])


              should list all names with their salaries.



              $max returns the highest salary per element. You could also choose $first etc, see https://docs.mongodb.com/manual/reference/operator/aggregation/group/#accumulator-operator.






              share|improve this answer































                1














                Mongo DB version > 3.4



                db.test.distinct("name")


                Mongo DB version < 3.4



                db.test.aggregate([{$group: {_id: "$name", salary: {$max: "$salary"}}}])





                share|improve this answer

































                  0














                  db.runCommand ( {
                  distinct: "CollectionName",
                  key: "key",
                  query: { "createdDate": {
                  $gte:new ISODate("2017-04-04T23:59:59Z"),
                  $lte:new ISODate("2017-04-25T23:59:59Z")}} } )


                  this query helps find data in collection , will fetch value of key from all documents which satisfy condition of between date






                  share|improve this answer


























                  • While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!

                    – Martin Tournoij
                    Apr 26 '17 at 23:37











                  • @Carpetsmoker - added comments ,hope this helps

                    – GSK
                    Apr 28 '17 at 9:07











                  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%2f20655506%2fget-distinct-records-values%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  14














                  You can use db.collection.distinct to get back an array of unique values:



                  > db.test.distinct("name")
                  [ "my_name", "john" ]





                  share|improve this answer




























                    14














                    You can use db.collection.distinct to get back an array of unique values:



                    > db.test.distinct("name")
                    [ "my_name", "john" ]





                    share|improve this answer


























                      14












                      14








                      14







                      You can use db.collection.distinct to get back an array of unique values:



                      > db.test.distinct("name")
                      [ "my_name", "john" ]





                      share|improve this answer













                      You can use db.collection.distinct to get back an array of unique values:



                      > db.test.distinct("name")
                      [ "my_name", "john" ]






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Dec 18 '13 at 10:30









                      Chris SeymourChris Seymour

                      62.9k20125162




                      62.9k20125162

























                          8














                          You can also use a distinct sentence with filtered collection. For example, you can get distinct values of names from salaries over 800 with the following query:



                          db.test.distinct("name", { "salary": { $gt: 800 } })





                          share|improve this answer




























                            8














                            You can also use a distinct sentence with filtered collection. For example, you can get distinct values of names from salaries over 800 with the following query:



                            db.test.distinct("name", { "salary": { $gt: 800 } })





                            share|improve this answer


























                              8












                              8








                              8







                              You can also use a distinct sentence with filtered collection. For example, you can get distinct values of names from salaries over 800 with the following query:



                              db.test.distinct("name", { "salary": { $gt: 800 } })





                              share|improve this answer













                              You can also use a distinct sentence with filtered collection. For example, you can get distinct values of names from salaries over 800 with the following query:



                              db.test.distinct("name", { "salary": { $gt: 800 } })






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Sep 7 '16 at 21:38









                              J.C. GrasJ.C. Gras

                              1,6031519




                              1,6031519























                                  4














                                  db.test.aggregate([{$group: {_id: "$name", salary: {$max: "$salary"}}}])


                                  should list all names with their salaries.



                                  $max returns the highest salary per element. You could also choose $first etc, see https://docs.mongodb.com/manual/reference/operator/aggregation/group/#accumulator-operator.






                                  share|improve this answer




























                                    4














                                    db.test.aggregate([{$group: {_id: "$name", salary: {$max: "$salary"}}}])


                                    should list all names with their salaries.



                                    $max returns the highest salary per element. You could also choose $first etc, see https://docs.mongodb.com/manual/reference/operator/aggregation/group/#accumulator-operator.






                                    share|improve this answer


























                                      4












                                      4








                                      4







                                      db.test.aggregate([{$group: {_id: "$name", salary: {$max: "$salary"}}}])


                                      should list all names with their salaries.



                                      $max returns the highest salary per element. You could also choose $first etc, see https://docs.mongodb.com/manual/reference/operator/aggregation/group/#accumulator-operator.






                                      share|improve this answer













                                      db.test.aggregate([{$group: {_id: "$name", salary: {$max: "$salary"}}}])


                                      should list all names with their salaries.



                                      $max returns the highest salary per element. You could also choose $first etc, see https://docs.mongodb.com/manual/reference/operator/aggregation/group/#accumulator-operator.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jul 3 '17 at 14:00









                                      serv-incserv-inc

                                      15.2k57289




                                      15.2k57289























                                          1














                                          Mongo DB version > 3.4



                                          db.test.distinct("name")


                                          Mongo DB version < 3.4



                                          db.test.aggregate([{$group: {_id: "$name", salary: {$max: "$salary"}}}])





                                          share|improve this answer






























                                            1














                                            Mongo DB version > 3.4



                                            db.test.distinct("name")


                                            Mongo DB version < 3.4



                                            db.test.aggregate([{$group: {_id: "$name", salary: {$max: "$salary"}}}])





                                            share|improve this answer




























                                              1












                                              1








                                              1







                                              Mongo DB version > 3.4



                                              db.test.distinct("name")


                                              Mongo DB version < 3.4



                                              db.test.aggregate([{$group: {_id: "$name", salary: {$max: "$salary"}}}])





                                              share|improve this answer















                                              Mongo DB version > 3.4



                                              db.test.distinct("name")


                                              Mongo DB version < 3.4



                                              db.test.aggregate([{$group: {_id: "$name", salary: {$max: "$salary"}}}])






                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Nov 18 '18 at 0:32









                                              Stephen Rauch

                                              30k153758




                                              30k153758










                                              answered Nov 18 '18 at 0:09









                                              Ramesh SeeraRamesh Seera

                                              413




                                              413























                                                  0














                                                  db.runCommand ( {
                                                  distinct: "CollectionName",
                                                  key: "key",
                                                  query: { "createdDate": {
                                                  $gte:new ISODate("2017-04-04T23:59:59Z"),
                                                  $lte:new ISODate("2017-04-25T23:59:59Z")}} } )


                                                  this query helps find data in collection , will fetch value of key from all documents which satisfy condition of between date






                                                  share|improve this answer


























                                                  • While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!

                                                    – Martin Tournoij
                                                    Apr 26 '17 at 23:37











                                                  • @Carpetsmoker - added comments ,hope this helps

                                                    – GSK
                                                    Apr 28 '17 at 9:07
















                                                  0














                                                  db.runCommand ( {
                                                  distinct: "CollectionName",
                                                  key: "key",
                                                  query: { "createdDate": {
                                                  $gte:new ISODate("2017-04-04T23:59:59Z"),
                                                  $lte:new ISODate("2017-04-25T23:59:59Z")}} } )


                                                  this query helps find data in collection , will fetch value of key from all documents which satisfy condition of between date






                                                  share|improve this answer


























                                                  • While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!

                                                    – Martin Tournoij
                                                    Apr 26 '17 at 23:37











                                                  • @Carpetsmoker - added comments ,hope this helps

                                                    – GSK
                                                    Apr 28 '17 at 9:07














                                                  0












                                                  0








                                                  0







                                                  db.runCommand ( {
                                                  distinct: "CollectionName",
                                                  key: "key",
                                                  query: { "createdDate": {
                                                  $gte:new ISODate("2017-04-04T23:59:59Z"),
                                                  $lte:new ISODate("2017-04-25T23:59:59Z")}} } )


                                                  this query helps find data in collection , will fetch value of key from all documents which satisfy condition of between date






                                                  share|improve this answer















                                                  db.runCommand ( {
                                                  distinct: "CollectionName",
                                                  key: "key",
                                                  query: { "createdDate": {
                                                  $gte:new ISODate("2017-04-04T23:59:59Z"),
                                                  $lte:new ISODate("2017-04-25T23:59:59Z")}} } )


                                                  this query helps find data in collection , will fetch value of key from all documents which satisfy condition of between date







                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited Jul 3 '17 at 13:50









                                                  serv-inc

                                                  15.2k57289




                                                  15.2k57289










                                                  answered Apr 26 '17 at 14:58









                                                  GSKGSK

                                                  25337




                                                  25337













                                                  • While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!

                                                    – Martin Tournoij
                                                    Apr 26 '17 at 23:37











                                                  • @Carpetsmoker - added comments ,hope this helps

                                                    – GSK
                                                    Apr 28 '17 at 9:07



















                                                  • While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!

                                                    – Martin Tournoij
                                                    Apr 26 '17 at 23:37











                                                  • @Carpetsmoker - added comments ,hope this helps

                                                    – GSK
                                                    Apr 28 '17 at 9:07

















                                                  While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!

                                                  – Martin Tournoij
                                                  Apr 26 '17 at 23:37





                                                  While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!

                                                  – Martin Tournoij
                                                  Apr 26 '17 at 23:37













                                                  @Carpetsmoker - added comments ,hope this helps

                                                  – GSK
                                                  Apr 28 '17 at 9:07





                                                  @Carpetsmoker - added comments ,hope this helps

                                                  – GSK
                                                  Apr 28 '17 at 9:07


















                                                  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%2f20655506%2fget-distinct-records-values%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 pass form data using jquery Ajax to insert data in database?

                                                  National Museum of Racing and Hall of Fame

                                                  Guess what letter conforming each word