Using $push in MLAB's MongoDB REST API

2019-08-26 22:15发布

问题:

I am using NodeJS to write a post query to insert an object in array through the MLAB REST API but I am getting error in making the API call.

The Json Object is like :

{
.....
"someattriute" : "somevalue",
"attributenew" : [ {"a":"a1","b":"b1"} ],
.....
},
{
....
}

The NodeJS API call is like :

request.post('https://api.mlab.com/api/1/databases/cabchain/collections/rides?q={"someattribute": "'+"somevalue"+'"}&apiKey=....',
                        { json: { "$push": {"attributenew": {"a": "a2", "b": "b2"}}
                        } },
                        function (error, response, body) {
                            if (!error && response.statusCode == 200) {
                                console.log("----->Insertion"+body);
                                return res.status(200).send("[{\"status\":\"success\"}]");
                            }
                            else
                            console.log("error.....");
                        });

The control goes in ELSE part and prints "error...." .

EDIT :

Printing error gives : null.
Printing response gives : [object object].
Printing response.statusCode : 400


{"statusCode":400,"body":{"message":"Invalid object { \"$push\" : { \"quoted\" : { \"driver\" : \"9820364381\" , \"fare\" : \"999\"}} , \"_id\" : { \"$oid\" : \"5ab1d191c2ef1674b9097f03\"}}: I
nvalid BSON field name $push"},"headers":{"date":"Wed, 21 Mar 2018 03:29:21 GMT","server":"Apache/2.4.7 (Ubuntu)","expires":"Tue, 01 Feb 2000 08:00:00 GMT","last-modified":"Wed, 21 Mar 2018 03
:29:21 GMT","cache-control":"no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0","pragma":"no-cache","x-frame-options":"DENY","access-control-allow-credentials":"true","
access-control-allow-origin":"*","content-length":"201","connection":"close","content-type":"application/json;charset=utf-8"},"request":{"uri":{"protocol":"https:","slashes":true,"auth":null,"
host":"api.mlab.com","port":443,"hostname":"api.mlab.com","hash":null,"search":"?q=%7B%22ridetrackingno%22:%20%22624%22%7D&apiKey=....","query":"q=%7B%22ridetrackin
gno%22:%20%22624%22%7D&apiKey=....","pathname":"/api/1/databases/DB/collections/rides","path":"/api/1/databases/cabchain/collections/rides?q=%7B%22ridetrackin
gno%22:%20%22624%22%7D&apiKey=....","href":"https://api.mlab.com/api/1/databases/DB/collections/rides?q=%7B%22ridetrackingno%22:%20%22624%22%7D&apiKey=....."},"method":"POST","headers":{"accept":"application/json","content-type":"application/json","content-length":57}}}