MongoDB wildcard in the key of a query












30















Is it possible to wildcard the key in a query? For instance, given the following record, I'd like to do a .find({'a.*': 4})
This was discussed here https://jira.mongodb.org/browse/SERVER-267 but it looks like it's not been resolved.



{
'a': {
'b': [1, 2],
'c': [3, 4]
}
}









share|improve this question

























  • i have the same issue with mongo. i have many documents like this: { one: { two: { more:data }, too: { more:data } } } once you're one level deep into the document tree, each node at the second level is a container for documents with consistent structure. so i would like to be able to search like this: find({'one.*.some.data':5})

    – rev
    Jan 5 '12 at 20:41


















30















Is it possible to wildcard the key in a query? For instance, given the following record, I'd like to do a .find({'a.*': 4})
This was discussed here https://jira.mongodb.org/browse/SERVER-267 but it looks like it's not been resolved.



{
'a': {
'b': [1, 2],
'c': [3, 4]
}
}









share|improve this question

























  • i have the same issue with mongo. i have many documents like this: { one: { two: { more:data }, too: { more:data } } } once you're one level deep into the document tree, each node at the second level is a container for documents with consistent structure. so i would like to be able to search like this: find({'one.*.some.data':5})

    – rev
    Jan 5 '12 at 20:41
















30












30








30


4






Is it possible to wildcard the key in a query? For instance, given the following record, I'd like to do a .find({'a.*': 4})
This was discussed here https://jira.mongodb.org/browse/SERVER-267 but it looks like it's not been resolved.



{
'a': {
'b': [1, 2],
'c': [3, 4]
}
}









share|improve this question
















Is it possible to wildcard the key in a query? For instance, given the following record, I'd like to do a .find({'a.*': 4})
This was discussed here https://jira.mongodb.org/browse/SERVER-267 but it looks like it's not been resolved.



{
'a': {
'b': [1, 2],
'c': [3, 4]
}
}






mongodb mongodb-query wildcard






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 9 '15 at 16:32









robsch

5,64654375




5,64654375










asked May 30 '11 at 19:04









BradBrad

2641414




2641414













  • i have the same issue with mongo. i have many documents like this: { one: { two: { more:data }, too: { more:data } } } once you're one level deep into the document tree, each node at the second level is a container for documents with consistent structure. so i would like to be able to search like this: find({'one.*.some.data':5})

    – rev
    Jan 5 '12 at 20:41





















  • i have the same issue with mongo. i have many documents like this: { one: { two: { more:data }, too: { more:data } } } once you're one level deep into the document tree, each node at the second level is a container for documents with consistent structure. so i would like to be able to search like this: find({'one.*.some.data':5})

    – rev
    Jan 5 '12 at 20:41



















i have the same issue with mongo. i have many documents like this: { one: { two: { more:data }, too: { more:data } } } once you're one level deep into the document tree, each node at the second level is a container for documents with consistent structure. so i would like to be able to search like this: find({'one.*.some.data':5})

– rev
Jan 5 '12 at 20:41







i have the same issue with mongo. i have many documents like this: { one: { two: { more:data }, too: { more:data } } } once you're one level deep into the document tree, each node at the second level is a container for documents with consistent structure. so i would like to be able to search like this: find({'one.*.some.data':5})

– rev
Jan 5 '12 at 20:41














1 Answer
1






active

oldest

votes


















11














As asked, this is not possible. The server issue you linked to is still under "issues we're not sure of".



MongoDB has some intelligence surrounding the use of arrays, and I think that's part of the complexity surrounding such a feature.



Take the following query db.foo.find({ 'a.b' : 4 } ). This query will match the following documents.



{ a: { b: 4 } }
{ a: [ { b: 4 } ] }


So what does "wildcard" do here? db.foo.find( { a.* : 4 } ) Does it match the first document? What about the second?



Moreover, what does this mean semantically? As you've described, the query is effectively "find documents where any field in that document has a value of 4". That's a little unusual.



Is there a specific semantic that you're trying to achieve? Maybe a change in the document structure will get you the query you want.






share|improve this answer
























  • thanks, that clarifies. More specifically, what I'm looking to do is wildcard just the specific node in the branch, i.e. any proper subfield of a. I'm not clear on how a.* says 'find where any field'. Isn't it 'find documents that have a top-level field 'a' with a subfield that matches 4'?

    – Brad
    Jun 1 '11 at 5:48











  • I think the confusion here is around "subfield". When I write {a:{b:4,c:2}}, I am saying that the value a is a JSON object. That JSON object has two keys b and c. The value of those keys are 4 & 2 respectively. When you are asking for a.*, you're effectively asking for syntax that loops through all of the keys in that JSON object. Your're not asking to "loop through an array", you're asking to "loop through an object's properties". That's a little unusual, which is why I'm asking for a specific use case here.

    – Gates VP
    Jun 1 '11 at 17:15






  • 2





    @gates: I have a use case. myDocInMongo = {'someUnknownKey':{propToCheck:true}, 'someKnownKey':true}; Now, I want to find this document using the selector {someKnownKey:{$exists:true}} but i also want to make sure that none of the other keys have an object with the property propToCheck. So, like the following: {'*.propToCheck':{$exists:false}, {someKnownKey:{$exists:true}}}

    – doubletap
    May 3 '12 at 17:57













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%2f6179871%2fmongodb-wildcard-in-the-key-of-a-query%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









11














As asked, this is not possible. The server issue you linked to is still under "issues we're not sure of".



MongoDB has some intelligence surrounding the use of arrays, and I think that's part of the complexity surrounding such a feature.



Take the following query db.foo.find({ 'a.b' : 4 } ). This query will match the following documents.



{ a: { b: 4 } }
{ a: [ { b: 4 } ] }


So what does "wildcard" do here? db.foo.find( { a.* : 4 } ) Does it match the first document? What about the second?



Moreover, what does this mean semantically? As you've described, the query is effectively "find documents where any field in that document has a value of 4". That's a little unusual.



Is there a specific semantic that you're trying to achieve? Maybe a change in the document structure will get you the query you want.






share|improve this answer
























  • thanks, that clarifies. More specifically, what I'm looking to do is wildcard just the specific node in the branch, i.e. any proper subfield of a. I'm not clear on how a.* says 'find where any field'. Isn't it 'find documents that have a top-level field 'a' with a subfield that matches 4'?

    – Brad
    Jun 1 '11 at 5:48











  • I think the confusion here is around "subfield". When I write {a:{b:4,c:2}}, I am saying that the value a is a JSON object. That JSON object has two keys b and c. The value of those keys are 4 & 2 respectively. When you are asking for a.*, you're effectively asking for syntax that loops through all of the keys in that JSON object. Your're not asking to "loop through an array", you're asking to "loop through an object's properties". That's a little unusual, which is why I'm asking for a specific use case here.

    – Gates VP
    Jun 1 '11 at 17:15






  • 2





    @gates: I have a use case. myDocInMongo = {'someUnknownKey':{propToCheck:true}, 'someKnownKey':true}; Now, I want to find this document using the selector {someKnownKey:{$exists:true}} but i also want to make sure that none of the other keys have an object with the property propToCheck. So, like the following: {'*.propToCheck':{$exists:false}, {someKnownKey:{$exists:true}}}

    – doubletap
    May 3 '12 at 17:57


















11














As asked, this is not possible. The server issue you linked to is still under "issues we're not sure of".



MongoDB has some intelligence surrounding the use of arrays, and I think that's part of the complexity surrounding such a feature.



Take the following query db.foo.find({ 'a.b' : 4 } ). This query will match the following documents.



{ a: { b: 4 } }
{ a: [ { b: 4 } ] }


So what does "wildcard" do here? db.foo.find( { a.* : 4 } ) Does it match the first document? What about the second?



Moreover, what does this mean semantically? As you've described, the query is effectively "find documents where any field in that document has a value of 4". That's a little unusual.



Is there a specific semantic that you're trying to achieve? Maybe a change in the document structure will get you the query you want.






share|improve this answer
























  • thanks, that clarifies. More specifically, what I'm looking to do is wildcard just the specific node in the branch, i.e. any proper subfield of a. I'm not clear on how a.* says 'find where any field'. Isn't it 'find documents that have a top-level field 'a' with a subfield that matches 4'?

    – Brad
    Jun 1 '11 at 5:48











  • I think the confusion here is around "subfield". When I write {a:{b:4,c:2}}, I am saying that the value a is a JSON object. That JSON object has two keys b and c. The value of those keys are 4 & 2 respectively. When you are asking for a.*, you're effectively asking for syntax that loops through all of the keys in that JSON object. Your're not asking to "loop through an array", you're asking to "loop through an object's properties". That's a little unusual, which is why I'm asking for a specific use case here.

    – Gates VP
    Jun 1 '11 at 17:15






  • 2





    @gates: I have a use case. myDocInMongo = {'someUnknownKey':{propToCheck:true}, 'someKnownKey':true}; Now, I want to find this document using the selector {someKnownKey:{$exists:true}} but i also want to make sure that none of the other keys have an object with the property propToCheck. So, like the following: {'*.propToCheck':{$exists:false}, {someKnownKey:{$exists:true}}}

    – doubletap
    May 3 '12 at 17:57
















11












11








11







As asked, this is not possible. The server issue you linked to is still under "issues we're not sure of".



MongoDB has some intelligence surrounding the use of arrays, and I think that's part of the complexity surrounding such a feature.



Take the following query db.foo.find({ 'a.b' : 4 } ). This query will match the following documents.



{ a: { b: 4 } }
{ a: [ { b: 4 } ] }


So what does "wildcard" do here? db.foo.find( { a.* : 4 } ) Does it match the first document? What about the second?



Moreover, what does this mean semantically? As you've described, the query is effectively "find documents where any field in that document has a value of 4". That's a little unusual.



Is there a specific semantic that you're trying to achieve? Maybe a change in the document structure will get you the query you want.






share|improve this answer













As asked, this is not possible. The server issue you linked to is still under "issues we're not sure of".



MongoDB has some intelligence surrounding the use of arrays, and I think that's part of the complexity surrounding such a feature.



Take the following query db.foo.find({ 'a.b' : 4 } ). This query will match the following documents.



{ a: { b: 4 } }
{ a: [ { b: 4 } ] }


So what does "wildcard" do here? db.foo.find( { a.* : 4 } ) Does it match the first document? What about the second?



Moreover, what does this mean semantically? As you've described, the query is effectively "find documents where any field in that document has a value of 4". That's a little unusual.



Is there a specific semantic that you're trying to achieve? Maybe a change in the document structure will get you the query you want.







share|improve this answer












share|improve this answer



share|improve this answer










answered May 31 '11 at 17:44









Gates VPGates VP

39.6k890105




39.6k890105













  • thanks, that clarifies. More specifically, what I'm looking to do is wildcard just the specific node in the branch, i.e. any proper subfield of a. I'm not clear on how a.* says 'find where any field'. Isn't it 'find documents that have a top-level field 'a' with a subfield that matches 4'?

    – Brad
    Jun 1 '11 at 5:48











  • I think the confusion here is around "subfield". When I write {a:{b:4,c:2}}, I am saying that the value a is a JSON object. That JSON object has two keys b and c. The value of those keys are 4 & 2 respectively. When you are asking for a.*, you're effectively asking for syntax that loops through all of the keys in that JSON object. Your're not asking to "loop through an array", you're asking to "loop through an object's properties". That's a little unusual, which is why I'm asking for a specific use case here.

    – Gates VP
    Jun 1 '11 at 17:15






  • 2





    @gates: I have a use case. myDocInMongo = {'someUnknownKey':{propToCheck:true}, 'someKnownKey':true}; Now, I want to find this document using the selector {someKnownKey:{$exists:true}} but i also want to make sure that none of the other keys have an object with the property propToCheck. So, like the following: {'*.propToCheck':{$exists:false}, {someKnownKey:{$exists:true}}}

    – doubletap
    May 3 '12 at 17:57





















  • thanks, that clarifies. More specifically, what I'm looking to do is wildcard just the specific node in the branch, i.e. any proper subfield of a. I'm not clear on how a.* says 'find where any field'. Isn't it 'find documents that have a top-level field 'a' with a subfield that matches 4'?

    – Brad
    Jun 1 '11 at 5:48











  • I think the confusion here is around "subfield". When I write {a:{b:4,c:2}}, I am saying that the value a is a JSON object. That JSON object has two keys b and c. The value of those keys are 4 & 2 respectively. When you are asking for a.*, you're effectively asking for syntax that loops through all of the keys in that JSON object. Your're not asking to "loop through an array", you're asking to "loop through an object's properties". That's a little unusual, which is why I'm asking for a specific use case here.

    – Gates VP
    Jun 1 '11 at 17:15






  • 2





    @gates: I have a use case. myDocInMongo = {'someUnknownKey':{propToCheck:true}, 'someKnownKey':true}; Now, I want to find this document using the selector {someKnownKey:{$exists:true}} but i also want to make sure that none of the other keys have an object with the property propToCheck. So, like the following: {'*.propToCheck':{$exists:false}, {someKnownKey:{$exists:true}}}

    – doubletap
    May 3 '12 at 17:57



















thanks, that clarifies. More specifically, what I'm looking to do is wildcard just the specific node in the branch, i.e. any proper subfield of a. I'm not clear on how a.* says 'find where any field'. Isn't it 'find documents that have a top-level field 'a' with a subfield that matches 4'?

– Brad
Jun 1 '11 at 5:48





thanks, that clarifies. More specifically, what I'm looking to do is wildcard just the specific node in the branch, i.e. any proper subfield of a. I'm not clear on how a.* says 'find where any field'. Isn't it 'find documents that have a top-level field 'a' with a subfield that matches 4'?

– Brad
Jun 1 '11 at 5:48













I think the confusion here is around "subfield". When I write {a:{b:4,c:2}}, I am saying that the value a is a JSON object. That JSON object has two keys b and c. The value of those keys are 4 & 2 respectively. When you are asking for a.*, you're effectively asking for syntax that loops through all of the keys in that JSON object. Your're not asking to "loop through an array", you're asking to "loop through an object's properties". That's a little unusual, which is why I'm asking for a specific use case here.

– Gates VP
Jun 1 '11 at 17:15





I think the confusion here is around "subfield". When I write {a:{b:4,c:2}}, I am saying that the value a is a JSON object. That JSON object has two keys b and c. The value of those keys are 4 & 2 respectively. When you are asking for a.*, you're effectively asking for syntax that loops through all of the keys in that JSON object. Your're not asking to "loop through an array", you're asking to "loop through an object's properties". That's a little unusual, which is why I'm asking for a specific use case here.

– Gates VP
Jun 1 '11 at 17:15




2




2





@gates: I have a use case. myDocInMongo = {'someUnknownKey':{propToCheck:true}, 'someKnownKey':true}; Now, I want to find this document using the selector {someKnownKey:{$exists:true}} but i also want to make sure that none of the other keys have an object with the property propToCheck. So, like the following: {'*.propToCheck':{$exists:false}, {someKnownKey:{$exists:true}}}

– doubletap
May 3 '12 at 17:57







@gates: I have a use case. myDocInMongo = {'someUnknownKey':{propToCheck:true}, 'someKnownKey':true}; Now, I want to find this document using the selector {someKnownKey:{$exists:true}} but i also want to make sure that none of the other keys have an object with the property propToCheck. So, like the following: {'*.propToCheck':{$exists:false}, {someKnownKey:{$exists:true}}}

– doubletap
May 3 '12 at 17:57






















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%2f6179871%2fmongodb-wildcard-in-the-key-of-a-query%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

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Fotorealismo