简体   繁体   中英

MongoError: key $get must not start with '$'

I am insert the object in mongo db at server side using node js and express js framework and fronted side I am use the angular js.But it is throw the error is that MongoError: key $get must not start with '$'

server.js

app
.use(bodyParser.json())
.post('/contact', upload.single('file'), function(req,res){


            var contact = req.body;
            delete contact.$promise;
            delete contact.$resolved;
            console.log(contact);
            //console.log(req.file);

            contact.userId = 1;

             db.collection(CONTACTS_COLLECTION).insertOne(contact, function(err, doc) {
                if (err) {
                  console.log(err + " Unsuccess");
                } else {
                  console.log(doc.ops[0]);
                  res.status(201).json(doc.ops[0]);
                }
              });

})

display value of contact at console

{ firstName: 'abc,text',
 lastName: 'abc,text',
 toJSON: 'function (){var a=v({},this);delete a.$promise;delete  a.$resolved;return a}',
'$get': 'function (a,b,d){x(a)&&(d=b,b=a,a=   {});a=m[c].call(this,a,this,b,d);return a.$promise||a}',
'$save': 'function (a,b,d){x(a)&&(d=b,b=a,a={});a=m[c].call(this,a,this,b,d);return a.$promise||a}',
'$query': 'function (a,b,d){x(a)&&(d=b,b=a,a={});a=m[c].call(this,a,this,b,d);return a.$promise||a}',
'$remove': 'function (a,b,d){x(a)&&(d=b,b=a,a={});a=m[c].call(this,a,this,b,d);return a.$promise||a}',
'$delete': 'function (a,b,d){x(a)&&(d=b,b=a,a={});a=m[c].call(this,a,this,b,d);return a.$promise||a}',
'$update': 'function (a,b,d){x(a)&&(d=b,b=a,a={});a=m[c].call(this,a,this,b,d);return a.$promise||a}' }

error

MongoError: key $get must not start with '$' Unsuccess

Thank you in advance

Are you seriously want to store the same contact which is being shown in console? with all the $command? or does it is getting appended undesired? What object you are sending from your front end?

As an answer for mongoDB $ is a reserved keyword and it should not be used inside the key of document you are trying to save. if they allow $ in keys, the queries would start failing as mongo uses $ keywords with special purposes, also it would be security threat similar to SQL injection.

As a solution, if you really want to store the contact with same data, I would suggest replace $get with get or _get and similarly all the keys with $ keyword.

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM