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();
})
});
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();
});
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/';
MongoClient.connect(url, {useNewUrlParser: true}, function(err, db){
if(err) throw err;
var dbo = db.db('mydb');
var myObj = [{name: 'Company Inc', address: 'Highway 37'}];
dbo.createCollection('Customers').insertone(myObj, function(err, res){
if(err) throw err;
console.log('1 document inserted!');
db.close();
});
});
My code is not working, the error is insertOne is not a function. My mongoDB shell version is 3.4.18 and npm installed mongodb module is 3.1.10.
Try inserting your record without creating the collection, as insertOne will automatically create the collection if it does not exist.
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/';
MongoClient.connect(url, {useNewUrlParser: true}, function(err, db){
if(err) throw err;
var dbo = db.db('mydb');
var myObj = [{name: 'Company Inc', address: 'Highway 37'}];
var collection = db.collection('Customers');
collection.insertOne(myObj, function(err, res){
if(err) throw err;
console.log('1 document inserted!');
db.close();
});
});
https://docs.mongodb.com/manual/reference/method/db.collection.insertOne/
just check the case of letter o in your insertOne function ..
I'm new with nodeJS and MongoDB.
I have this code:
var fs = require('fs');
var mongojs = require('mongojs');
var db = mongojs('monitor', ["configurations"]);
fs.readFile('json/object1.json', 'utf8', function (err, data) {
if (err) throw err;
console.log(data);
db.configurations.insert(data, function(err, doc) {
console.log(data);
if(err) throw err;
});
});
no data insered to the mongodb, and I've got no error.
the both console.log(data) print the json string as well.
Try to parse it as JSON before inserting it into the document
fs.readFile('json/object1.json', 'utf8', function (err, data) {
if (err) throw err;
console.log(data);
var json = JSON.parse(data);
db.configurations.insert(json, function(err, doc) {
console.log(data);
if(err) throw err;
});
});
I have an issue with the asynchrous programming model in Node.js.
I know this has been asked before, but so far I haven't found anything that I can use to solve my issue.
How do I force console.log from running before dbQueryName has been invoked and username has gotten its value from it?
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
function dbQueryName() {
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("mydb");
dbo.collection("users").findOne({}, function(err, result) {
if (err) throw err;
return result.name
db.close();
});
});
}
var username = dbQueryName();
// Wait until dbQueryName() has been invoked and username has gotten its value before running this
console.log(username);
I have managed to get it working using a callback parameter in the function, but is there a way to do this with less code and indentation?
function dbQueryName(callback) {
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("mydb");
dbo.collection("users").findOne({}, function(err, result) {
if (err) throw err;
callback(result.name)
db.close();
});
});
}
dbQuery(function(result){
var username = result
console.log(username);
});
Yes if you are not passing a callback to MongoClient.connect or collection.findOne, it returns a promise.
But you need to add another function to use async await
init()
async function dbQueryName() {
const db = await MongoClient.connect(url);
const dbo = db.db("mydb");
const user = await dbo.collection("users").findOne({})
return user.name
}
async function init() {
try {
const name = await dbQueryName()
} catch (error) {
console.log(error);
}
}
I am not able to insert data into MongoDB database using insert method in Nodejs.
var url = 'mongodb://localhost:27017/learnyoumongo';
var mongo = require('mongodb').MongoClient;
mongo.connect(url, function(err, db) {
if (err) throw err;
// db gives access to the database
const myDb = db.db('learnyoumongo');
var docs = myDb.collection('docs');
var obj = {firstName: process.argv[2], lastName: process.argv[3]};
docs.insert(obj, function(err, res){
if(err) throw err;
console.log('data inserted');
})
db.close();
}
There is no output coming and connection to the data base is successful but no insertion of data is happening.
Try this will work for you .
var url = 'mongodb://localhost:27017/learnyoumongo';
var mongo = require('mongodb').MongoClient;
mongo.connect(url, function(err, dbobj) {
if (err) throw err;
// db gives access to the database
// const myDb = dbobj.db('learnyoumongo').collection('docs');
// var docs = myDb.collection('docs');
var obj = {firstName: process.argv[2], lastName: process.argv[3]};
dbobj.db('learnyoumongo').collection('docs').insert(obj, function(err, res){
if(err) throw err;
console.log('data inserted');
dbobj.close();
})
})
I figured out what the problem is. Seems like I forgot to add a parenthesis in the end:
var url = 'mongodb://localhost:27017/learnyoumongo';
var mongo = require('mongodb').MongoClient;
mongo.connect(url, function(err, db) {
if (err) throw err;
// db gives access to the database
const myDb = db.db('learnyoumongo');
var docs = myDb.collection('docs');
var obj = {firstName: process.argv[2], lastName: process.argv[3]};
docs.insert(obj, function(err, res){
if(err) throw err;
console.log('data inserted');
})
db.close();
})
You can use the following code to insert data into MongoDB using nodejs.
var mongo = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/learnyoumongo";
mongo.connect(url, function(err, db) {
if (err) throw err;
//access to the database
var myobj = { firstname: "Jhon", lastname: "Mackey" };
db.collection("docs").insertOne(myobj, function(err, res) {
if (err) throw err;
console.log("Data inserted");
db.close();
});
});