When attempting to update a mongoDB collection with an array as the value, the update fails silently.
This does not work:
var arr = ["test","test1","test2"];
$.ajax('http://my.mongodb.com/collection?id=80a2c727de877ac9' , {
type: "PUT",
contentType: "application/json",
data: JSON.stringify({
mykey: arr
}),
success: function() {
// Do something
},
error: function(xhr) {
console.log(xhr.responseText);
}
});
This does:
$.ajax('http://my.mongodb.com/collection?id=80a2c727de877ac9' , {
type: "PUT",
contentType: "application/json",
data: JSON.stringify({
mykey: "test"
}),
success: function() {
// Do something
},
error: function(xhr) {
console.log(xhr.responseText);
}
});
It turns out I need to stringify the array before stringifying it within the ajax data:
var arr = ["test","test1","test2"];
arr = JSON.stringify(arr);
$.ajax('http://mordor.fmr.com:8030/techtest?id=80a2c727de877ac9' , {
type: "PUT",
contentType: "application/json",
data: JSON.stringify({
assignedTechs: arr
}),
success: function() {
// Do something
},
error: function(xhr) {
console.log(xhr.responseText);
}
});
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.