skip to Main Content

MongoDB Aggregation – Flatten resultat

db.collection.aggregate([ .... { $group: {_id: "$data.r.id", "col": {$first: "$data.r"}}}, {$project:{"o":{"$objectToArray":"$col"}}}, {$unwind:"$o"}, {$group:{"_id":null, "keys":{$addToSet:"$o.k"}}}, {$project: {"keys":1, "_id": 0}}, {$addFields: {res:{$map:{ input:"$keys", as: "a", in: {"_id":0,"label": "$$a"}}}}}, {$project: {"res":1}} ]) With my MongoDB query, the output is: { "res" : [ {…

VIEW QUESTION

Adding multiple fields to documents in mongodb using pymongo

I have a sample collection of documents in mongo db like below [{"name":"hans","age":30,"test":"pass","pre":"no","calc":"no"}, {"name":"abs","age":20,"test":"not_pass","pre":"yes","calc":"no"}, {"name":"cdf","age":40,"test":"pass"}, {"name":"cvf","age":30,"test":"not_pass","pre":"no","calc":"yes"}, {"name":"cdf","age":23,"test":"pass"}, {"name":"asd","age":35,"test":"not_pass"}] For some documents the fields pre and calc are not present. I want to add those two fields to the documents which…

VIEW QUESTION

How to perform conditional arithmetic operations in MongoDB

I've following schema { "_id" : ObjectId("xxxxx"), "updatedAt" : ISODate("2022-06-29T13:10:36.659+0000"), "createdAt" : ISODate("2022-06-29T08:06:51.264+0000"), "payments" : [ { "paymentId" : "xxxxx", "paymentType" : "charge", "paymentCurrency" : "PKR", "paymentMode" : "cash", "paymentTotal" : 13501.88, "penalties" : 100 }, { "paymentId" : "ccccc",…

VIEW QUESTION
Back To Top
Search