Piet
Piet

Reputation: 415

Convert MongoDB query result into JSONArray

I have a MongoDB query via mongoose and the result is:

[
  {
    "tablename": "name1"
  },
  {
    "tablename": "name2"
  }
]

but I need it as JSONArray :

{
  "tablename": [
    "name1",
    "name2"
  ]
}

Is there an easy way to convert?

Upvotes: 1

Views: 2635

Answers (3)

chridam
chridam

Reputation: 103325

The aggregation framework can create the desired result for you. Consider the $group and the $push accumulator operators as follows:

db.collection.aggregate([
    {
        "$group": {
            "_id": null,
            "tablename": {
                "$push": "$tablename"
            }           
        }
    },
    {
        "$project": {
            "_id": 0, "tablename": 1
        }
    }    
])

Sample Output:

/* 0 */
{
    "result" : [ 
        {
            "tablename" : [ 
                "name1", 
                "name2"
            ]
        }
    ],
    "ok" : 1
}

You can implement this in mongoose using the aggregate() method, for example:

var pipeline = [
        {
            "$group": {
                "_id": null,
                "tablename": {
                    "$push": "$tablename"
                }           
            }
        },
        {
            "$project": {
                "_id": 0, "tablename": 1
            }
        }    
    ]

Model.aggregate(pipeline).exec(function (err, res){
    console.log(res);
})

UPDATE

Change this query to use the aggregation framework:

app.post('/getkost', function(request, response){ 
    var kost = new RegExp(request.body.kost,'i'); 
    Move.find(
        { tablename: { $regex: kost } },
        { tablename: 1, _id: 0 }, 
        function(err, doc) { 
            response.json(doc); 
    }); 
});

to this:

app.post('/getkost', function(request, response){ 
    var kost = new RegExp(request.body.kost, 'i'); 
    var pipeline = [
        {
            "$match": {
                "tablename": { "$regex": kost }
            }
        },
        {
            "$group": {
                "_id": null,
                "tablename": {
                    "$push": "$tablename"
                }           
            }
        },
        {
            "$project": {
                "_id": 0, "tablename": 1
            }
        }
    ]
    Move.aggregate(pipeline, function(err, res) { 
        response.json(res); 
    }); 
});

Upvotes: 1

brandonscript
brandonscript

Reputation: 72845

This should work for you:

var json = [{
    "tablename": "name1"
}, {
    "tablename": "name2"
}]

var arr = {};
json.forEach(function(value, key) {
    var tableName = Object.keys(json[key]);
    arr[tableName] = arr[tableName] || [];
    arr[tableName].push(value[tableName]);
});

console.log(arr);

Fiddle

Keep in mind that Object.keys() will actually return an array - if your array contains more than one key (e.g. {"tablename": "name1", "othertablename": "name2"})

Upvotes: 0

WhiteHat
WhiteHat

Reputation: 61212

try...

var inputArr = [
  {
    "tablename": "name1"
  },
  {
    "tablename": "name2"
  }
];

var jsonArr = {"tablename": []};

for (var i = 0; i < inputArr.length; i++) {
  jsonArr.tablename.push(inputArr[i].tablename);
}

Upvotes: 0

Related Questions