1

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?

3 Answers 3

1

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); 
    }); 
});
Sign up to request clarification or add additional context in comments.

9 Comments

its working for a find query too? I dont want to change any data
Can you show the find() query? You can substitute that query with the $match pipeline operator.
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); }); });
@Piet Check out my update, this should give you the desired result using the aggregation framework.
"$match" : { SyntaxError: Unexpected token :
|
0

try...

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

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

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

Comments

0

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"})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.