My code looks like this:
#!/bin/env node
var collection = require('mongojs')('test').collection('test');
collection.findOne({}, function(err, doc) {
console.log(err, doc);
});
When I ran this script, it showed:
$ node test.js
null null
But the script didn't quit. I need to "CTRL+C" to quit it. Any body know how to fix it?
[update]
I found that if I use native mongodb instead of mongojs, there's no problem:
#!/bin/env node
var client = require('mongodb');
client.connect('mongodb://127.0.0.1:27017/hatch', function(err, db) {
if (err) throw err;
var collection = db.collection('documents');
collection.find().toArray(function(err, results) {
console.dir(results);
db.close();
});
});
So is it a mongojs issue?
You have to close the db connection
var mongojs = require('mongojs');
var db = mongojs('test');
var collection = db.collection('test');
collection.findOne({}, function(err, doc) {
console.log(err, doc);
db.close();
});
Related
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();
})
});
In all tutorials I searched the web, they print the rows/result using console.log the from the mysql query. In my case, I want to store that value to manipulate it in a function. The problem is that node doesn't wait. Can you help me in catching the rows in such example? How can I store the rows in a variable?
con.connect(function (err) {
if (err) throw err;
con.query("SELECT * FROM customers", function (err, result) {
if (err) throw err;
console.log(result);
});
});
I tested one of the solutions but I got [] when trying to get result/rows out of the function. I want to use the result in the savedServices: [] see below:
Give a fresh start with a simple project
Follow below commands
mkdir testdb
cd testdb
npm install --save mysql
npm install --save async
Create a test.js file
vim text.js
Now put below code in the test.js file
var mysql = require('mysql');
var async = require('async');
var con = mysql.createConnection({
host: "localhost",
database: "mydb",
user: "user",
password: "pass"
});
var myrows = [];
async.waterfall([
function(callback) {
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = 'select * from users limit 5';
con.query(sql, function (err, result) {
if (err) throw err;
result.forEach(function(item) {
//console.log(item);
myrows.push(item);
});
callback(null, myrows);
});
});
}
],
// optional callback
function(err, myrows) {
console.log("myrows:::");
console.log(myrows);
});
now test the file
node test.js
You should see results like this:
Connected!
Result: [object Object],[object Object],[object Object],[object Object],[object Object]
Hope this helps!
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();
});
});
If I run this using node it prints "Connected to Database":
var MongoClient = require("mongodb").MongoClient;
MongoClient.connect("mongodb://localhost/db1", function(err, db) {
if (err) {
throw err;
}
console.log("Connected to Database");
db.close();
});
However if I try to run it with a Grunt task it does nothing and silently.
module.exports = function(grunt) {
return grunt.registerTask("task", "subtask", function() {
var MongoClient = require("mongodb").MongoClient;
return MongoClient.connect("mongodb://localhost/db1", function(err, db) {
if (err) {
throw err;
}
console.log("Connected to Database");
db.close();
});
});
};
Can anyone tell me why this should be and perhaps provide a workaround?
Everything works as it should be.
Database connections are async and therefore grunt "kills off" you task before the connection is ever established.
You task should look like this:
module.exports = function(grunt) {
return grunt.registerTask("task", "subtask", function() {
var done = this.async();
var MongoClient = require("mongodb").MongoClient;
MongoClient.connect("mongodb://localhost/db1", function(err, db) {
if (err) {
done(false);
}
console.log("Connected to Database");
db.close();
done();
});
});
};
There is an entire section in the grunt documentation about this: Why doesn't my asynchronous task complete?
I'm trying to get the result of query but i get the same info in all vars: db, collection and res:
var mongodb = require("mongodb");
var mongoserver = new mongodb.Server("localhost", 27017);
var instance = new mongodb.Db("test", mongoserver);
instance.open(function(err, db)
{
console.log('db:');
console.log(db);
db.collection('kurtsoa', function(err, collection)
{
console.log('collection:');
console.log(collection);
collection.find({}, function(err, res)
{
console.log('res:');
console.log(res);
});
});
});
how i can get the result of "find"?
.find() will return a Cursor object for you to work with. If all you are interested in is getting all the results in an array you can do:
collection.find().toArray(function(err, docs) {
console.log(docs);
});
But you can also iterate the cursor too:
collection.find().each(function(err, doc) {
//called once for each doc returned
});
You can use this:
collection.find().toArray(function(err, docs){
console.log(docs);
)};