Bula
Bula

Reputation: 2792

Inserting in mongodb with nodejs

I'm trying to insert some data in my mongodb with nodejs whenever a socket is emitted. Here is the code:

io.sockets.on( "connection",function( socket ){
    socket.on( "send", function( data ) {
        console.log(data.name + " and the content is: " + data.content);
        mongodb.connect( "mongodb://127.0.0.1", function( err, db ) {
            if(err) throw err;
            var to_be_inserted = {name: data.name,content: data.content};
            db.collection("chat").insert(to_be_inserted,function(err,objects){
                if(err) throw err;
            });
        })
    })
})

However whenever I go to my mongo console and type

db.chat.find() 

I cannot find the inserted record. I'm sure that I have mongod open and I'm sure that the socket is emitted. Moreover the consoloe.log before the insertion does work.

Here is my mongo client

var mongodb = require("mongodb").MongoClient;

My console which runs the nodejs server does not log any error.

Upvotes: 1

Views: 939

Answers (4)

Niranjan Samunuri
Niranjan Samunuri

Reputation: 1

const http = require('http');
const hostname = '127.0.0.1';
const port = 8081;
var express = require("express");
var bodyParser = require('body-parser');
var app = express();
var MongoClient = require('mongodb').MongoClient;
   var url = "mongodb://localhost:27017";


app.use(bodyParser.json());

app.get('/get', function (req, res) {
  res.send('Hello World')
})

var data = {
  title: 'my title',
  content: 'my content'
};
// This responds a POST request for the homepage
app.post('/say/:userid', function (req, res) {
 
   var queryParameter=JSON.stringify(req.query);
   res.send('Hello POST'+req.params.userid+""+queryParameter);
})

app.post('/insert', function (req, res) {
 
   console.log(req.body);
   res.send('Hello POST'+JSON.stringify(req.body));
 /*   var MongoClient = require('mongodb').MongoClient;
   var url = "mongodb://localhost:27017"; */
  MongoClient.connect(url, function(err, db) {
  if (err) throw err;
 var dbase = db.db("mydb");
  var myobj = { name: JSON.stringify(req.body.name), address:JSON.stringify(req.body.address) };
  dbase.collection("student").insertOne(myobj, function(err, res) {
    if (err) throw err;
    console.log("1 document inserted");

Upvotes: 0

nox0311
nox0311

Reputation: 76

try this code

    var MongoClient=require('mongodb').MongoClient;
var Server=require('mongodb').Server;
var mongoc=new MongoClient(new Server("localhost",27017));
mongoc.open(function(err)
    {

    db.collection(<collection_name>).insert(<query>,function(err,result)
{

});

Upvotes: 0

heinob
heinob

Reputation: 19474

You should specify a database name (here: myDatabase ) and a port number (for safety).

mongodb.connect("mongodb://127.0.0.1:27017/myDatabase", function( err, db ) {

When searching the record in the mongo shell try:

use myDatabase
db.chat.find()

Upvotes: 2

Balayesu Chilakalapudi
Balayesu Chilakalapudi

Reputation: 1406

You forget to include the port number and database of mongodb,

MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) {
if (err) throw err;
console.log("Connected to Database");
}

Upvotes: 0

Related Questions