mongodb query and return specific collections
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
add a comment |
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
Use.dot
notationdb.users.find({ "profile.grade": "Grade Two" })
and for projectiondb.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
add a comment |
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
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
mongodb mongodb-query
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
notationdb.users.find({ "profile.grade": "Grade Two" })
and for projectiondb.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
add a comment |
Use.dot
notationdb.users.find({ "profile.grade": "Grade Two" })
and for projectiondb.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
add a comment |
1 Answer
1
active
oldest
votes
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 })
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%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
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 })
add a comment |
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 })
add a comment |
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 })
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 })
answered Nov 25 '18 at 16:28
Anthony WinzletAnthony Winzlet
15.4k41339
15.4k41339
add a comment |
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%2f53468888%2fmongodb-query-and-return-specific-collections%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
Use
.dot
notationdb.users.find({ "profile.grade": "Grade Two" })
and for projectiondb.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