how to create Auto increment for serial number in nodejs mongodb

4,186 views
Skip to first unread message

Sankaravelu S

unread,
Jun 13, 2012, 11:59:12 PM6/13/12
to mongod...@googlegroups.com
Hi,

I am using mongodb 2.0.6 and nodejs 0.6.19.

Below program,

     I had one html page for user to enter the phone_number and keyword.

     In this keyword and Phone_number is stored into Replica set through the mongos server 27021.

     Now i want to create auto increment field for serial number(sno).

     Please help me....



var http = require('http'),qs = require('querystring'),dateFormat = require('dateFormat');

http.createServer(function (req, res) {

  res.writeHead(200, {'Content-Type': 'text/html'});

  res.write("<html><body><form action='' method='POST'><table><tr><td><label for='phonenumber'>Phonenumber</label></td><td><input type='text' name='phonenumber' id='phonenumber'/></td></tr><tr><td><label for='keyword'>Keyword</label> </td><td><input type='text' name='keyword' id='keyword'/></td></tr><tr><td><input type='submit' value='Submit'> </input></td></tr></table></form></body></html>");

  if(req.method=='POST') {
            var body='';
            req.on('data', function (data) {
                body += data;
            });
           
            req.on('end',function(){
                var ph_value =  qs.parse(body);
               
                var Db = require('mongodb').Db;
                var Server = require('mongodb').Server;
               
                var client = new Db('test', new Server('192.168.1.51', 27021, {}));
               
                var insertData = function(err, collection){
                        collection.save({"Phonenumber":ph_value.phonenumber,"Keyword":ph_value.keyword,"sno":1, "Date":dateFormat(new Date(), "dd-mm-yyyy HH:MM:ss")});
                            break;
                        }
                }
               
                client.open(function(err, pClient){
                         client.collection('demo', insertData);
                       
                });

            });
  }
  res.end();
}).listen(8000, '127.0.0.1');
console.log('Server running at http://127.0.0.1:8000/');


Thanks in advance.
Sankaravelu.S

Sam Millman

unread,
Jun 14, 2012, 3:15:30 AM6/14/12
to mongod...@googlegroups.com
This should give you the theory of how to do what you want: http://www.mongodb.org/display/DOCS/How+to+Make+an+Auto+Incrementing+Field

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Sankaravelu S

unread,
Jun 16, 2012, 3:49:49 AM6/16/12
to mongod...@googlegroups.com
Hi Sam,

I will use the auto increment field for serial number(sno) in my below coding.


var insertData = function(err, collection){
                        while( 1 ) {
                            var c = collection.find({},{sno:1}).sort({sno:-1}).limit(1);
                            var i = hasNext() ? c.next().sno + 1 : 1;

                            collection.save({"Phonenumber":ph_value.phonenumber,"Keyword":ph_value.keyword,"sno":i,"Date":dateFormat(new Date(), "dd-mm-yyyy HH:MM:ss")});
                            break;
                        }
                }

It will give the error.

C:\Program Files\nodejs>node sample.js

Server running at http://127.0.0.1:8000/

C:\Program Files\nodejs\sample.js:23
                                                var i = hasNext() ? c.next().sno
 + 1 : 1;
                     ^
ReferenceError: hasNext is not defined
    at C:\Program Files\nodejs\sample.js:23:22
    at Db.collection (C:\Program Files\nodejs\node_modules\mongodb\lib\mongodb\d
b.js:447:44)
    at C:\Program Files\nodejs\sample.js:30:18
    at C:\Program Files\nodejs\node_modules\mongodb\lib\mongodb\db.js:247:16
    at [object Object].<anonymous> (C:\Program Files\nodejs\node_modules\mongodb
\lib\mongodb\connection\server.js:383:7)
    at [object Object].emit (events.js:88:20)
    at [object Object].<anonymous> (C:\Program Files\nodejs\node_modules\mongodb
\lib\mongodb\connection\connection_pool.js:93:15)
    at [object Object].emit (events.js:70:17)
    at Socket.<anonymous> (C:\Program Files\nodejs\node_modules\mongodb\lib\mong
odb\connection\connection.js:385:10)
    at Socket.emit (events.js:67:17)




Thanks in advance.
Sankaravelu.S

Mike O'Brien

unread,
Jun 29, 2012, 2:34:08 PM6/29/12
to mongodb-user
It looks like you copied/pasted some code from that example page, but
the javascript in that example is for the mongo shell, not the NodeJS
driver, which is why hasNext() doesn't work here. You will need to
modify it so that it uses the callback function semantics.
In the node driver, .find() returns a cursor which you must then use
with .toArray(), .each() or .nextObject() to step through each
document.

On Jun 16, 3:49 am, Sankaravelu S <san...@hexolabs.com> wrote:
> Hi Sam,
>
> I will use the auto increment field for *serial number(sno)* in my below
> coding.
>
> var insertData = function(err, collection){
>                         while( 1 ) {
>                             *var c =
> collection.find({},{sno:1}).sort({sno:-1}).limit(1);
>                             var i = hasNext() ? c.next().sno + 1 : 1;*
>
> collection.save({"Phonenumber":ph_value.phonenumber,"Keyword":ph_value.keyw ord,"sno":i,"Date":dateFormat(new
> Date(), "dd-mm-yyyy HH:MM:ss")});
>                             break;
>                         }
>                 }
>
> *It will give the error.*
>
> C:\Program Files\nodejs>node sample.js
> Server running athttp://127.0.0.1:8000/
>
> C:\Program Files\nodejs\sample.js:23
>                                                 var i = hasNext() ?
> c.next().sno
>  + 1 : 1;
>                      ^
> *ReferenceError: hasNext is not defined*
>     at C:\Program Files\nodejs\sample.js:23:22
>     at Db.collection (C:\Program
> Files\nodejs\node_modules\mongodb\lib\mongodb\d
> b.js:447:44)
>     at C:\Program Files\nodejs\sample.js:30:18
>     at C:\Program Files\nodejs\node_modules\mongodb\lib\mongodb\db.js:247:16
>     at [object Object].<anonymous> (C:\Program
> Files\nodejs\node_modules\mongodb
> \lib\mongodb\connection\server.js:383:7)
>     at [object Object].emit (events.js:88:20)
>     at [object Object].<anonymous> (C:\Program
> Files\nodejs\node_modules\mongodb
> \lib\mongodb\connection\connection_pool.js:93:15)
>     at [object Object].emit (events.js:70:17)
>     at Socket.<anonymous> (C:\Program
> Files\nodejs\node_modules\mongodb\lib\mong
> odb\connection\connection.js:385:10)
>     at Socket.emit (events.js:67:17)
>
> Thanks in advance.
> Sankaravelu.S
>
>
>
>
>
>
>
> On Thu, Jun 14, 2012 at 12:45 PM, Sam Millman <sam.mill...@gmail.com> wrote:
> > This should give you the theory of how to do what you want:
> >http://www.mongodb.org/display/DOCS/How+to+Make+an+Auto+Incrementing+...
>
> > On 14 June 2012 04:59, Sankaravelu S <san...@hexolabs.com> wrote:
>
> >> Hi,
>
> >> I am using mongodb 2.0.6 and nodejs 0.6.19.
>
> >> Below program,
>
> >>      I had one html page for user to enter the phone_number and keyword.
>
> >>      In this keyword and Phone_number is stored into Replica set through
> >> the mongos server 27021.
>
> >>      Now i want to create *auto increment field for serial number*(sno).
> >> *"**sno":1*, "Date":dateFormat(new Date(), "dd-mm-yyyy HH:MM:ss")});
> >>                             break;
> >>                         }
> >>                 }
>
> >>                 client.open(function(err, pClient){
> >>                          client.collection('demo', insertData);
>
> >>                 });
>
> >>             });
> >>   }
> >>   res.end();
> >> }).listen(8000, '127.0.0.1');
> >> console.log('Server running athttp://127.0.0.1:8000/');
Reply all
Reply to author
Forward
0 new messages