con.connect(function(err){
if(err) throw err;
var sql="Insert into Employee(name) values('Pravin')";
con.query(sql, function(err, result){
if(err)throw err;
/*Use the result object to get id*/
console.log("1 record inserted , ID"+result.insertedID);
});
});
1 record inserted , IDundefined
not able get Id number in this
Related
How can I delete particular data in mongodb using node.js ?
router.post('/deletedata', (req, res) => {
console.log("deleted values are",req.body.id)
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("mohan");
var myquery = req.body.id;
console.log("myquery value is:", myquery)
dbo.collection("customers").remove({myquery}, function(err, obj) {
if (err) throw err;
db.close();
});
});
res.json({
statusCode: 200,
result: "success",
})
}
);
export default router;
I got particular id from React hooks crud app , So i can see the id in node js but it does not delete the that particular id data in mongoDB
Your query will only delete documents with myquery: passedId
I bet query should look like {_id: myquery}
dbo.collection("customers").remove({_id: myquery}, function(err, obj) {
if (err) throw err;
db.close();
});
dbo.collection("customers").remove(myquery, function(err, obj) {
if (err) throw err;
db.close();
});
try using delete({query}) or deleteMany({query})
My code:
MongoClient.connect(gloabl_vars.db.mongo.url,function(err, db) {
if(err) { throw err; }
var dbo=db.db("profilemanager");
var mquery={_id:'123454'};
db.collection('userinfo').find(mquery,{'_id':0,'subscriptions':1}).toArray(function(err,result){
if(err) throw err;
console.log(result);
});
});
}
am able to get the result from Robo3T mongo client but same is returning null through nodejs.
Robo3T:
db.getCollection('userinfo').find({_id:'66613'},{'_id':0,'subscriptions':1});
You are searching a record by {_id:'66613'} in Robo3T but your sample is {_id:'123454'} in node.js. Also projection in node.js find is not in this way. Try below Snippet
MongoClient.connect(gloabl_vars.db.mongo.url,function(err, db) {
if(err) { throw err; }
var dbo=db.db("profilemanager");
var mquery={_id:'66613'};
db.collection('userinfo').find(mquery).project({'_id':0,'subscriptions':1}).toArray(function(err,result){
if(err) throw err;
console.log(result);
});
});
}
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.
I'm trying to find() by _id using Node.js and MongoDB.
I'm able to show objects in the database when using an alternative parameter, or using no parameters at all. Here's my code and results to show that:
mongo.connect(url, function(err, db){
if (err) throw err;
db.collection('listings').find().toArray(function(err, result){
if (err) throw err;
console.log(result);
db.close();
});
});
The results I get back are this:
[{"_id":"5a1ff1ac32fe0a2bd966be1e","title":"hi","price":"5"},{"_id":"5a207cea2f119cbd6a5fa688","title":"hello","price":"10"}]
When I modify the code to find the results using the title "hi" I get back this:
[{"_id":"5a1ff1ac32fe0a2bd966be1e","title":"hi","price":"5"}]
using the following code:
mongo.connect(url, function(err, db){
if (err) throw err;
db.collection('listings').find({'title': 'hi'}).toArray(function(err, result){
if (err) throw err;
console.log(result);
db.close();
});
});
However when I try to sort by _id using this code:
mongo.connect(url, function(err, db){
if (err) throw err;
db.collection('listings').find({'_id': '5a1ff1ac32fe0a2bd966be1e'}).toArray(function(err, result){
if (err) throw err;
res.send(result);
db.close();
});
});
The results I get back is
[]
The question is: how do I find by _id so that when I use _id I can get back results for that specific _id?
'5a1ff1ac32fe0a2bd966be1e' is a string.
_id is of type ObjectId.
You can make an object id with ObjectID('5a1ff1ac32fe0a2bd966be1e'). You can require the function like this:
const ObjectID = require('mongodb').ObjectID;
Here is the documentation.
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!!!!!!!!!!!!!!!!!!!!!!!!!!!!
});
});