Get distinct records values
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
add a comment |
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
add a comment |
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
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
mongodb
edited Dec 18 '13 at 10:32
Chris Seymour
62.9k20125162
62.9k20125162
asked Dec 18 '13 at 10:24
GelidusGelidus
5451719
5451719
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
You can use db.collection.distinct to get back an array of unique values:
> db.test.distinct("name")
[ "my_name", "john" ]
add a comment |
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 } })
add a comment |
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.
add a comment |
Mongo DB version > 3.4
db.test.distinct("name")
Mongo DB version < 3.4
db.test.aggregate([{$group: {_id: "$name", salary: {$max: "$salary"}}}])
add a comment |
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
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
You can use db.collection.distinct to get back an array of unique values:
> db.test.distinct("name")
[ "my_name", "john" ]
add a comment |
You can use db.collection.distinct to get back an array of unique values:
> db.test.distinct("name")
[ "my_name", "john" ]
add a comment |
You can use db.collection.distinct to get back an array of unique values:
> db.test.distinct("name")
[ "my_name", "john" ]
You can use db.collection.distinct to get back an array of unique values:
> db.test.distinct("name")
[ "my_name", "john" ]
answered Dec 18 '13 at 10:30
Chris SeymourChris Seymour
62.9k20125162
62.9k20125162
add a comment |
add a comment |
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 } })
add a comment |
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 } })
add a comment |
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 } })
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 } })
answered Sep 7 '16 at 21:38
J.C. GrasJ.C. Gras
1,6031519
1,6031519
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jul 3 '17 at 14:00
serv-incserv-inc
15.2k57289
15.2k57289
add a comment |
add a comment |
Mongo DB version > 3.4
db.test.distinct("name")
Mongo DB version < 3.4
db.test.aggregate([{$group: {_id: "$name", salary: {$max: "$salary"}}}])
add a comment |
Mongo DB version > 3.4
db.test.distinct("name")
Mongo DB version < 3.4
db.test.aggregate([{$group: {_id: "$name", salary: {$max: "$salary"}}}])
add a comment |
Mongo DB version > 3.4
db.test.distinct("name")
Mongo DB version < 3.4
db.test.aggregate([{$group: {_id: "$name", salary: {$max: "$salary"}}}])
Mongo DB version > 3.4
db.test.distinct("name")
Mongo DB version < 3.4
db.test.aggregate([{$group: {_id: "$name", salary: {$max: "$salary"}}}])
edited Nov 18 '18 at 0:32
Stephen Rauch
30k153758
30k153758
answered Nov 18 '18 at 0:09
Ramesh SeeraRamesh Seera
413
413
add a comment |
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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