Mongo db returning empty results - node.js

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);
});
});
}

Related

Mongodb find not printing json data

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();
});

Delete particular data in mongodb using node js

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})

NodeJs And MongoDB

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'm unable to find by _id on mongodb

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.

MongoError: Connection Closed By Application using node.js driver

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!!!!!!!!!!!!!!!!!!!!!!!!!!!!
});
});

Resources