Piet Piet - 6 months ago 27
Javascript Question

MongoDB query is JSONArray instead JSONObject

I want to select a whole document and send it as JSONObject.

app.post('/getinvbykost', function(request, response){
var tablename = request.body.tablename;
Move.find({tablename: tablename}, function(err, doc) {
response.json(doc);
});
});


This gives the correct result but with "[]" ->Array and not with {} ->JSONObject. btw: I got the same issue with
Move.aggregate(pipeline, function(err, res) {...


result:

[
{
STUFF
}
]


There is a function $unwind but I dont get it....

Answer

In this case since you only want one result, the findOne method would be more appropriate.

app.post('/getinvbykost', function(request, response){ 
    var tablename = request.body.tablename; 
    Move.findOne({tablename: tablename}, function(err, doc) {
        response.json(doc);
    }); 
});