2014-05-12 12 views
0

尝试了差不多2个小时后,我张贴了这个问题无法在MongoDB中的记录进行更新

$.ajax({ 
type: "POST" , 
url: "http://127.0.0.1:28017/test/stocks/?q={symbol:"+symbol+"}", 
contentType: "application/json; charset=utf-8", 
data: JSON.stringify({ "$set" : { 'tbq' : "3" } }), 
dataType: "json" 
}); 

内MongoDB的样本

{ 
     "_id" : ObjectId("537148b99aae10a52d6b5dbe"), 
     "symbol" : "KIYT", 
     "tbq" : "566" 

} 

有谁请电话我为什么更新ISN”工作?

我以下链接http://docs.mongolab.com/restapi/#update-documents

回答

0

你需要做一个PUT而不是一个POST,因为POST将尝试一个新的项目添加到蒙戈。您还需要提供您尝试更新的项目的_id。例如;

$.ajax({ 
    type: "PUT" , 
    url: 'http://127.0.0.1:28017/test/stocks/?q={"_id":537148b99aae10a52d6b5dbe}', 
    contentType: "application/json", 
    data: JSON.stringify({ "$set" : { 'tbq' : "3" } }), 
    dataType: "json" 
}); 
+0

感谢您的回答,但仍然无法正常更新。 – Pawan

+0

如果我使用PUT而不是POST,我进入我的mongodb控制台[websvr]不知道如何处理[PUT] – Pawan

+1

道歉我没有意识到你正在尝试在本地安装上执行此操作(我应该有看URL!)mongolabs的REST API允许插入和更新,但是mongod的API不。请参阅http://docs.mongodb.org/manual/core/security-interface/#rest-api – mattdavis90