I am looking for a Mongo query for finding data in the following document.
{
"key1": [{
"subkey1": ["america.south.gas"],
"subkey2": ["9898989898"]
}],
"key2": [{
"subkey1": ["america"],
"subkey2": ["hsadjsahjsahdjsah879878u9"]
},
{
"subkey1": ["america.south.gas","america"],
"subkey2": ["hsadjsahjsahdjsah879878u9"]
},
{
"subkey1": ["america.south.#"],
"subkey2": ["sjadkjsahdkjsahdj989s89d8sa98d9sa"]
}]
}
I want only subkey2 only of above mentioned document as following output:
"subkey2": ["hsadjsahjsahdjsah879878u9"]
"subkey2": ["sjadkjsahdkjsahdj989s89d8sa98d9sa"]
Now I want to fetch data with following query:-
db.collectionName.find({$or:[
{"key2.subkey1": "america.south.gas"},
{"key1.subkey1": "america.south.#"}
]},
{"_id": 0, "key2.subkey2.$": 1}
);
But it is showing me this error:
{
"waitedMS" : NumberLong(0),
"ok" : 0,
"errmsg" : "Executor error during find command: BadValue: positional operator (key1.$) requires corresponding field in query specifier",
"code" : 96
}
Any idea how can I achieve this for getting specific field with multiple query field in Mongo find operation??
javatag, is this relevant? Are you trying to express this query using the Mongo Java driver? Or using the Mongo shell?