here is the code im trying to run in linux.
I've tried changing my bind ip to 0.0.0.0 and to 127.0.0.1 and even commented it but was no help. The error it displays is in screenshot here.
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var myobj = { name: "Company Inc", address: "Highway 37" };
db.collection("customers").insertOne(myobj, function(err, res) {
if (err) throw err;
console.log("1 record inserted");
db.close();
});
});
error details
Collection#insertOne() requires mongodb#2, so if possible, upgrade your driver.
If not, you can use Collection#insert().
Related
Hello I have issue with MongoDB code, it make some methods like this (image below):
enter image description here
`
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("mydb");
var mysort = { name: 1 };
dbo.collection("customers").find().sort(mysort).toArray(function (err, result) {
if (err) throw err;
console.log(result);
db.close();
});
});
`
and when I try read error, it say:
(method) MongoClient.connect(url: string, callback: Callback): void (+3 overloads)
#deprecated — Callbacks are deprecated and will be removed in the next major version. See mongodb-legacy for migration assistance
The signature '(url: string, callback: Callback): void' of 'MongoClient.connect' is deprecated.ts(6387)
mongodb.d.ts(4733, 9): The declaration was marked as deprecated here.
No quick fixes available
I tried reinstall it, or install older version and still same, I do connection same like in documentation.
Tried add (url,{ useUnifiedTopology: true }, and still same
Someone know what can be issue?
Try version 4.0.0
const MongoClient = require('mongodb').MongoClient;
// Connect to the MongoDB server
MongoClient.connect('mongodb://localhost:27017/', function(err, client) {
if (err) throw err;
// Get the sample database
const db = client.db('sample');
// Get the collection
const collection = db.collection('test');
// Insert a document
collection.insertOne({ name: 'John', age: 30 }, function(err, result) {
if (err) throw err;
console.log(result);
// Close the connection
client.close();
});
});
Let me know if you still have the same error .
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();
});
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 am trying to connect to MongoDB with user/password , this is what I did so far:
var MongoClient = require('mongodb').MongoClient;
// Connect to the db
MongoClient.connect("mongodb://${host}:27017/${db}", function(err, db) {
if(!err) {
console.log("successfully connected to the database");
}else{
console.log("Error on connecting... aborting and exiting");
return console.dir(err);
throw err;
}
db.authenticate('username', 'password', function(err, res) {
console.log("reached here");
});
});
Now I am trying to login inside the data base in order to be able to get inside Mongo Database's collections, how can I do that?
Thanks!
You can perform CURD operations like following:
var MongoClient = require('mongodb').MongoClient;
// Connect to the db
MongoClient.connect("mongodb://${host}:27017/${db}", function(err, db) {
if(!err) {
console.log("successfully connected to the database");
//here you can perform operation
var collection = db.collection('test');
var doc1 = {'hello':'doc1'};
var doc2 = {'hello':'doc2'};
collection.insert(doc1);
}else{
console.log("Error on connecting... aborting and exiting");
return console.dir(err);
throw err;
}
db.authenticate('username', 'password', function(err, res) {
console.log("reached here");
});
});
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();
});