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

connection problem mongoDB with MongoClient

I have some problem about connection to my mongoDB with MongoClient. const { MnogoClient , ObjectID} = require('mongodb') const id = new ObjectID() console.log(id) const connectionURL = 'mongodb://127.0.0.1:27017' const databaseName = 'task-manager' MnogoClient.connect(connectionURL , {useNewUrlParser: true}, (error, client)=>{ if(error){ return…

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
Back To Top
Search