E.g.: FileName = Lakshmikantha.html and Username also Lakshmikantha. If that Lakshmikantha.html file is modified then I want to delete that user from DB.
app.js
app.post('/home',urlencodedParser,function(req1,res1){
var filname = req1.body.username;
var str;
var originalfile;
var flag;
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("mydb");
dbo.collection("users").insert(req1.body, function(err, res) {
if (err) throw err;
console.log("user "+req1.body.username+ " inserted");
// db.close();
});
dbo.collection("users").find().toArray(function(err, result) {
if (err) throw err;
res1.render('home',{user:result});
// db.close();
});
watcher.on('change', function(path1) {
var filename = path.basename(path1);
originalfile = pathParse(filename).name;
str = originalfile.toString();
console.log(originalfile);
flag = true;
});
db.close();
});
if(flag){
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("mydb");
dbo.collection("users").remove({"username":str}, function(err, obj) {
if (err) throw err;
console.log("1 document deleted" + str);
});
db.close();
});
}
});
/*MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("mydb");
//Delete the "customers" collection:
dbo.collection("users").drop(function(err, delOK) {
if (err) throw err;
if (delOK) console.log("Collection deleted");
db.close();
});
});*/
Here I am inserting users to DB through loginpage and if particular filename with that username appears, I want to delete the entry from the DB.
Related
In the code I am trying to find all documents with code UE19CS204.But in the console.log
a big message is printed not the JSON data.The findOne() is working but not find().
I don’t know what change to do to find all documents with code UE19CS204.
var MongoClient = require(‘mongodb’).MongoClient;
var url = “mongodb://localhost:27017/”;
MongoClient.connect(url, { useUnifiedTopology: true } ,function(err, db) {
if (err) throw err;
var dbo = db.db(“pes”);
dbo.collection(“course”).find({“code”:“UE19CS204”}, function(err, result) {
if (err) throw err;
console.log(result);
});
dbo.collection(“course”).findOne({code:“UE19CS204”}, function(err, result) {
if (err) throw err;
console.log(result);
db.close();
});
});
The method find() creates a cursor for a query that can be used to iterate over results from MongoDB, see here.
Use toArray(), you can finde the documentation here.
dbo.collection(“course”).find({“code”:“UE19CS204”}).toArray(function(err, docs) {
if (err) {
throw err;
}
console.log(docs);
})
Full example:
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'pes';
// Collection Name
const collectionName = 'course';
// Filter
const filter = { 'code': 'UE19CS204' }
// Use connect method to connect to the server
MongoClient.connect(url, { useUnifiedTopology: true }, function(err, client) {
if (err) {
throw err;
}
client.db(dbName).collection(collectionName).find(filter).toArray(function(err, docs) {
if (err) {
throw err;
}
console.log(docs);
})
client.close();
});
I use follow codes to insert something into mongodb, and if it is successfully insertted, I should get string with information success
if (email) {
MongoClient.connect(dbUrl, { useNewUrlParser: true }, function(err, db) {
if (err) throw err;
const dbo = db.db("email");
const dbObj = {email: email};
dbo.collection("email").insertOne(dbObj, function(err, res) {
if (err) throw err;
ctx.body = "success";
db.close();
});
});
} else {
ctx.body = "email is missing";
}
And now the document can be inserted into mongodb, but the get 404 Not Found, any ideas?
db.getCollection('Leave').find({},{_id:0 ,
Can_It_Be_carry_forwarded:1})
this is working perfectly in the MongoDb Client CMD But not in the Below Code
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/Chatbot_Project";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("Chatbot_Project");
dbo.collection('Leave').find({}, {Can_It_Be_carry_forwarded:1}).toArray(function(err, result) {
if (err)
throw err;
console.log(result);
db.close();
})
});
Your problem is the find method, you are missing the projection field.
If you want to retrive only the Can_It_Be_carry_forwarded field you need the following: {projection:{Can_It_Be_carry_forwarded:1, _id: 0}} as the second argument.
Solution from a similar question: https://stackoverflow.com/a/48294672/4120554
Documentation: http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#find
Try this:
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/Chatbot_Project";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("Chatbot_Project");
dbo.collection('Leave').find({},{projection:{_id: 0, Can_It_Be_carry_forwarded:1}}).toArray(function(err, result) {
if (err)
throw err;
console.log(result);
db.close();
})
});
I would like to get the whole document instead of the added item when I do a save().
var newTodo = Todos({
ID: req.body.ID,
RuleName: req.body.RuleName
});
newTodo.save(function (err, todos) {
if (err) throw err;
res.send(todos);
});
You cannot get it, unless you extend model method, or get it inside save
Simple version
newTodo.save(function (err, todos) {
if (err) throw err;
Todos.find(err, todos) {
if (err) throw err;
res.send(todos);
}
});
Version with custom method
// in schema definition
TodosSchema.methods.saveAndFind = function(cb) {
var self = this;
self.save(function(err) {
if(err) throw err;
return self.model('Todos').find({}, cb);
})
};
// in controller
var newTodo = Todos({
ID: req.body.ID,
RuleName: req.body.RuleName
});
newTodo.saveAndFind(function (err, todos) {
if (err) throw err;
res.send(todos);
});
Hi guys, need some help , have a problem when run this code :
MongoClient.connect('mongodb://localhost:27017/school',function (err,db) {
if(err) throw err;
var query = {};
var cursor = db.collection('students').find(query);
cursor.each(function (err,doc) {
if(err) throw err;
if(doc==null) return db.close();
//Processing doc to update
db.collection('students').update({"_id":doc["_id"]},{$set:{"scores":doc.scores}},function (err,result) {
if(err) throw err;
});
});
it works, but then appear this message =(:
MongoError: Connection Closed By Application
MongoClient.connect('mongodb://localhost:27017/school',function (err,db) {
if(err) throw err;
var query = {};
var cursor = db.collection('students').find(query);
cursor.each(function (err,doc) {
if(err) throw err;
if(doc==null) return db.close();
//Processing doc to update
db.collection('students').update({"_id":doc["_id"]},{$set:{"scores":doc.scores}},function (err,result) {
if(err) throw err;
db.close(); //this line was missing!!!!!!!!!!!!!!!!!!!!!!!!!!!!
});
});