Saving field result of aggregation unwind in collection - mongodb

I need save the field idOriginAccount in collection banking_atendimento.
the result of aggregate operation returns
I'm using robo3t to make the query
see the query
db.getCollection('banking_atendimento').aggregate([{
$lookup:{
from:"banking_ted",
localField:"idtransacao",
foreignField:"id",
as:"transacao",
}
},
{$unwind: '$transacao'},
])
What operator i do use to insert the field in collection "banking_ted"

As Andrews suggested, you can use $out to write to a new collection.
Here's the complete query for you
db.getCollection('banking_atendimento').aggregate([{
$lookup:{
from:"banking_ted",
localField:"idtransacao",
foreignField:"id",
as:"transacao",
}
},
{$unwind: '$transacao'},
{$out: 'banking_ted'}
])

Related

How to write group by query for embedded array of document in mongodb

I have a collection in below format
{customerID:1,acctDetails:[{accType:"Saving",balance:100},{accType:"checking",balance:500}]}
{customerID:2,acctDetails:[{accType:"Saving",balance:500}]}
I want to find total balance by acctType. I tried below query.
db.<collectionName>.aggregate([{$group:{_id:"$acctDetails.accType",totalBalance:{$sum:"$accDetails.balace"}}}])
But it is not giving right result.
I think that this might solve your problem. You first need to use $unwind to transform each array element in a document, then use $group to sum the total balance by account type.
db.collection.aggregate([
{"$unwind": "$acctDetails"},
{
"$group": {
"_id": "$acctDetails.accType",
"totalBalance": {"$sum": "$acctDetails.balance"}
}
}
])
Working Mongo playground

how to find data in array of object in mongodb

I find in array of object and match _id and is_active both key
ex
{
_id:'12333333333333'
name:'test',
array:[{
id:'1233449',
is_active:true
},{
id:'7987979',
is_active:false
},{
id:'9558555',
is_active:true
},{
id:'2564654',
is_active:false
}]
}
find data using mongo query
db.getCollection('demo').find({'array.id':'7987979','array.is_active':false});
not working
The reason it isnt working for you is because you are running a find operation on the array directly.
When querying for things within an array you can use $elemMatch to obtain an entire document that contains the matching array element.
If you need a custom output you can use Aggregation, with the $unwind operation in the pipeline on the array field.
Try this out on your collection and understand what it is doing
db.collectionName.aggregate([{
$unwind:"$array"
},{
$match:{
$and:[
{"array.id":'your_id'},
{"array.is_active":boolean}
]
}
}
])

MongoDB query, filter using cursor

I have two collections, one that has _id and UserId, and another that has UserId (same unique identifier) and "other data".
I want to filter the latter collection based on a list of _ids from the former collection.
Can someone provide an example query for this scenario?
The only way to 'join' collections in MongoDB is a $lookup aggregation stage (available in version 3.2).
firstCollection.aggregate([
{ $match: { _id: {$in: [1,2,3] }}}, // filter by _ids
{
$lookup:
{
from: "secondCollection",
localField: "UserId",
foreignField: "UserId",
as: "data"
}
}
])
That will add 'data' field to the documents from the first collection which will contain all related documents from second collection. If relation is not 1:1, you can add $unwind stage to flatten results:
{$unwind: "$data"}

Using $match on computed field Mongodb

I have this aggregation query in MongoDB:
db.questions.aggregate([
{ $project:{question:1,detail:1, choices:1, answer:1,
percent_false:{
$multiply:[100,{$divide:["$answear_false",{$add:["$answear_false","$answear_true"]}]}]},
percent_true:{
$multiply:[100,{$divide:["$answear_true",{$add:["$answear_false","$answear_true"]}]}]} }}, {$match:{status:'active'} }
]).pretty()
I want using $match on 2 computed fields "percent_true" and "percent_false" like this
$match : {percent_true:{$gte:20}}
How can i do ?
Singe the aggregation framework works in stages, you can treat the computed fields as if they were normal fields because from the $match's perspective, they are normal.
{ $project:{
question:1,detail:1, choices:1, answer:1,
percent_false:{
$multiply:[100,{$divide:["$answear_false",{$add:["$answear_false","$answear_true"]}]}]
},
percent_true:{
$multiply:[100,{$divide:["$answear_true",{$add:["$answear_false","$answear_true"]}]}]}
}
},
{$match:{
status:'active',
percent_true:{$gte:20}
//When documents get fed to match they already have a percent_true field, so you can match on them as normal
}
}

Retrieve Array Of Documents in MongoDB

I have a MongoDB Document like as follows
{
"_id":1,
"name":"XYZ"
ExamScores:[
{ExamName:"Maths", UnitTest:1, Score:100},
{ExamName:"Maths", UnitTest:2, Score:80},
{ExamName:"Science", UnitTest:1, Score:90}
]
}
I Need to retrieve this document so that it has to show only Maths Array. Like as follows
{
"_id":1,
"name":"XYZ"
ExamScores:[
{ExamName:"Maths", UnitTest:1, Score:100},
{ExamName:"Maths", UnitTest:2, Score:80},
]
}
How Can I Do That ?
As #karin states there is no, normal, in query method of doing this.
In version 2.2 you can use $elemMatch to project the first matching result from ExamScores but you cannot get multiple.
That being said, the aggregation framework can do this:
db.col.aggregate([
{$unwind: '$ExamScores'},
{$match: {'ExamScores.ExamName':"Maths"}},
{$group: {_id: '$_id', name: '$name', ExamScores: {$push: '$ExamScores'}}}
])
Something like that anyway.
This has been asked before MongoDB query to limit values based on condition, the only answer there says it is not possible, but that there is a request to implement that.