Find Object of Object MongoDB not returning any values - mongodb

I have this data structure:
{
"_id" : ObjectId("582ecaa97be792282ca31bc4"),
"hero" : {
"5001" : {
"id" : 5001
"name" : "Rogue"
}
"5002" : {
"id" : 5002
"name" : "Mage"
}
"5003" : {
"id" : 5002
"name" : "Paladin"
}
}
}
I have the query.
db.getCollection('hero').find({"Hero":{"5001":{"id" : 5001}}})
It returns 0 results.
I could store the Heros separately, but I would like to know how to do this query first. What I would like to get out of this is all data under 5001 if I query with 5001 and so forth.
Thanks!
Edit: I found the answer by using dot notation, I already tried dot notation but i got it a bit wrong when trying it, I thought it was only for arrays. Here is what you would do if anyone else is looking.
db.Runes.find({ "hero.5001.id": 5001 },{"hero.5001":1})

First of all, All the fields are case sensitive so you cannot query a "hero" field with "Hero".
second if you are querying nested documents you should use the "dot notation"
https://docs.mongodb.com/manual/core/document/#dot-notation
so the proper way to your that document will be.
db.getCollection('hero').find({"hero.5001.id" : 5001})
it will return the entire document. So entire hero object with 5002 and 5003 key will also return as they belong to the same document. You can use projection to project only the required fields.
which can be done as below
db.getCollection('hero').find({"hero.5001.id": 5001},{"hero.5001" : 1})
Here are more information about Mongodb query and projection operators
https://docs.mongodb.com/v3.2/reference/operator/query/

Just try this
db.getCollection('hero').find({"hero.5001.id" : 5001})

Related

MongoDB Query: $all with nested property

Hello I need some help with MongoDB:
My Document has the following property (generated by an Map with spring):
"filter":{"billingAccount_id":["multisim5"],"simulate":["true"]}
and i'm trying to find the document with the this code (generated by spring)
query.addCriteria(Criteria.where("filter").all(getMapWithValues()));
which results in
"filter" : { "$all" : [{ "billingAccount_id" : ["multisim5"] }] }
but i'm getting no result. What's to do here?
Thanks for help in advance
You're applying $all on an object (filter) instead of an array (filter.billingAccount_id). Change your request (and method getMapWithValues) to get something like :
{"filter.billingAccount_id" : { "$all" : ["multisim5"] }}
BUT $all is used to find array that contains, at least, all values provided in query. Since there's an unique element in $all param, $all is useless here, and the following query output the same result :
{"filter.billingAccount_id" : "multisim5"}
EDIT : if you want to query array with exact match ("multisim5" and nothin else), use the following :
{"filter.billingAccount_id" : { "$eq" : ["multisim5"] }}

MongoDB Compound text search

I have a collection like this in my mongo database, let's say it's called taxonomic.
{
"_id" : ObjectId("5810e15a762a39b41912a131"),
"validName" : "Eros",
"idUser" : ObjectId("1")
}
{
"_id" : ObjectId("5810e15a762a39b41912a132"),
"validName" : "Eros",
"idUser" : ObjectId("2")
}
I've already created a compound index to be able to search for the two values I want, such as this.
db.taxonomic.createIndex({"idUser":1,"validName":1})
Now, I want to be able to search and get a return from it only when both of the parameters are found on the same document of the collections, here's my try:
db.taxonomic.find({$text:{$search:"Eros 2"}},{idUser:1,validName:1})
The problem with this method is that it will return any match of "Eros" OR "2", what I want is a return of the values when "Eros" AND "2" are matched in a document of the collection.
Thank you for any help!
I dont think you require a text Index for it if you only want specific string
db.taxonomic.find({"$or" : [{"validName" : "Eros"},{"validName" : "2"}]},{idUser:1,validName:1})

Mongo db : query on nested json

Sample json object :
{ "_id" : ObjectId( "55887982498e2bef5a5f96db" ),
"a" : "x",
"q" : "null",
"p" : "",
"s" : "{\"f\":{\"b\":[\"I\"]},\"time\":\"fs\"}" }
need all documents where time = fs
My query :
{"s":{"time" : "fs"}}
above returns zero products but that is not true.
There are two problems here. First of all s is clearly a string so your query cannot work. You can use $regex as below but it won't be very efficient:
{s: {$regex: '"time"\:"fs"'}}
I would suggest converting s fields to proper documents. You can use JSON.parse to do it. Documents can be updated based on a current value using db.foo.find().snapshot().forEach. See this answer for details.
Second problem is your query is simply wrong. To match time field you should use dot notation:
{"s.time" : "fs"})

MongoDB - How to find equals in collection and in embedded document

Gurus - I'm stuck in a situation that I can't figure out how I can query from the following collection "spouse", which has embedded document "surname" and check for equality with "surname" of this document:
{
"_id" : ObjectId("50bd2bb4fcfc6066b7ef090d"),
"name" : "Gwendolyn",
"surname" : "Davis",
"birthyear" : 1978,
"spouse" : {
"name" : "Dennis",
"surname" : "Evans",
"birthyear" : 1969
},
I need to query:
Output data for all spouses with the same surnames (if the surname of
one of the spouses is not specified, assume that it coincides with the
name of another)
I tried something like this:
db.task.find( {"surname" : { "spouse.surname" : 1 }} )
but it failed)
PLEASE PLEASE Guide me how I can achieve this any example/sample? based on this will be really helpful :-)
Thanks a lot!
You have three options.
Use $where modifier:
db.task.find({$where: 'this.spouse.surname === this.surname'})
Update all your documents and add special flag. After that you will be able to query documents by this flag. It's faster then $where, but requires altering your data.
Use MapReduce. It's quite complicated, but it allows you to do nearly anything.

Mongo Map Dot notation

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"
}
}
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:
Use the new MongoDB Aggregation Framework. This has a $project operation that should do what you're looking for.
You can return just the 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?