如何使用node.js从mongodb检索和显示数据

时间:2014-12-08 12:11:44

标签: json node.js mongodb mongojs

JSON:

"name" : "fhj",
"age" : "23",
"gender" : "female",
"sec" : "b",
"username" : "9886666",
"language" : "HINDI",
"method" : "method2",
"timeSlot" : {
    "id" : 2,
    "fromTime" : 12,
    "toTime" : 15
}

我使用mongoJS进行数据库连接。 mycode的:

db.users.find(function(err,values){
if(err||!values.length)
   console.log("ERROR !!!!");
else
{ 
   var i=0;
   str='[';
   var str;
   values.forEach(function(user){
     if(i==values.length-1)
         str=str+'{ "name" : "' + user.username + '","age" : "'+ user.age +'","gender":"'+user.gender+'","sec":"'+user.sec+'","username":"'+user.username+'","language":"'+user.language+'","method":"'+user.method+'","toTime":"'+user.timeSlot.to+'"}';
     else{
       str = str + '{ "name" : "' + user.username + '","age" : "'+ user.age +'","gender":"'+user.gender+'","sec":"'+user.sec+'","username":"'+user.username+'","language":"'+user.language+'","method":"'+user.method+'","toTime":"'+user.timeSlot.to+'"},' +'\n';
       i++;
     }
      str=JSON.parse(user);

   });
  str = str.trim();
   str = str + ']';
 }  
});
}

我想使用包含json的node.js从mongodb中检索和显示数据,就像上面的文件一样。该怎么做?

0 个答案:

没有答案