先前说过我已将文档插入到mongo集合中.
MongoClient.connect(url, function(err,db){
if(err) {throw err;}
else {
document = {action: "alert",
protocol: "udp",
port: "80",
_id: "12" }
var collection = db.collection("connections");
collection.insertOne(document, function(err,result){
if (err) {throw err;}
else {
console.log("Successful")
db.close();
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想更新协议字段.到目前为止我没有运气的是
MongoClient.connect(url, function(err,db){
if (err) { throw err; }
else {
var collection = db.collection("connections");
collection.findOneAndUpdate({_id: "12"}, {$set: {protocol: "http"}}, {new: true}, function(err,doc) {
if (err) { throw err; }
else { console.log("Updated"); }
});
}
});
Run Code Online (Sandbox Code Playgroud)
我是否将错误的参数传递给findOneAndUpdate方法?我正确连接到数据库.
所以我有一个控制器,其方法定义为
controller1.js
router.get('/:id/:field', function(req,res){
var regex = /action|protocol|ip|port|direction|dip|dport|signature/;
if (regex.test(req.params.field)){
get(req,res,function(r){
var field = req.params.field;
res.status(200).send(r[field]);
});
} else {
res.status(404).send("Signature Field Does Not Exist");
}
});
function get(req, res, cb){
MongoClient.connect(url, function(err, db) {
if (err){
console.error("Could not connect to database: %s",err);
res.sendStatus(500);
} else {
var _id = req.params.id
var collection = db.collection("signatures");
var uniqueID = {"_id":_id};
var cursor = collection.find(uniqueID);
cursor.hasNext(function (err, r) {
if (err) {console.log(err);}
else {
cursor.next(function(err,r) {
if (r == null){
res.status(404).send("Signature not …Run Code Online (Sandbox Code Playgroud)