Reputation: 836
Document looks like this.
{
'tid': 1,
'matches': [{
'dord': 1,
'matches': [{
'tord': 1,
'score': 11
},
{
'tord': 2,
'score': 12
}
]
},
{
'dord': 2,
'matches': [{
'tord': 1,
'score': 21
},
{
'tord': 2,
'score': 22
}
]
}]
}
You can see that it is a nested array. I want to extract the element at matches.1.matches.0 . Result should look like this:
{
'tord': 2,
'score': 22
}
How do I do this ?
Upvotes: 0
Views: 54
Reputation: 295
Use the following method
exports.findScore = async (tid,tord,dord)=>{
return await tournament.findOne({tid:tid},{matches:{$elemMatch:{tord:tord},{dord:dord}}});
};
Upvotes: 0
Reputation: 8695
Bellow are 3 ways
The main problem is that we cant use .index
in path.
Query1 (move forward with temp variables)
db.collection.aggregate([
{
"$set": {
"member": {
"$let": {
"vars": {
"m1": {
"$arrayElemAt": [
"$matches",
1
]
}
},
"in": {
"$arrayElemAt": [
"$$m1.matches",
1
]
}
}
}
}
},
{
"$project": {
"member": 1
}
}
])
Query2 (move forward with temp fields)
db.collection.aggregate([
{
"$set": {
"member": {
"$arrayElemAt": [
"$matches",
1
]
}
}
},
{
"$set": {
"member": {
"$arrayElemAt": [
"$member.matches",
1
]
}
}
},
{
"$project": {
"member": 1
}
}
])
Query3 (move forward to project the documents,move backwards for the arrays "$arrayElemAt")
db.collection.aggregate([
{
"$set": {
"member": {
"$arrayElemAt": [
{
"$arrayElemAt": [
"$matches.matches",
1
]
},
1
]
}
}
},
{
"$project": {
"member": 1
}
}
])
The more natural are the 1,2 i think because you only move forward, but there is option 3 also.
Which is the faster i dont know for sure, i think the 2.
Upvotes: 1