Reputation: 86
I would like to construct a query that will return just the names of the classes for the datastructure below. So far, the closest I've come is using dot notation
db.mycoll.find({name:"game1"},{"classes.1.name":true})
But the problem with this approach is that it will only return the name of the first class. Please help me get the names of all three classes.
I wish I could use a wild card as below, but I'm not sure if that exists.
db.mycoll.find({name:"game1"},{"classes.$*.name":true})
Datastructure:
{
"name" : "game1",
"classes" : {
"1" : {
"name" : "warlock",
"version" : "1.0"
},
"2" : {
"name" : "shaman",
"version" : "2.0"
},
"3" : {
"name" : "mage",
"version" : "1.0"
}
}
Upvotes: 1
Views: 1536
Reputation: 45277
There is no simple query that will achieve the results you seek. MongoDB has limited support for querying against sub-objects or arrays of objects. The basic premise with MongoDB is that you are querying for the top-level document.
That said, things are changing and you still have some options:
$project
operation that should do what you're looking for.classes
field and then merge the names
together. This should be trivial with most languages.Note, that it's not clear what you're doing with classes
. Is this an array or an object? If it's an object, what does classes.1
actually represent? Is it different from classes.warlock
?
Upvotes: 1