我想在mongodb中显示特定日期的出席情况

时间:2015-02-07 10:42:57

标签: angularjs node.js mongodb

我有这样的架构:

               var UserSchema   = new Schema({
             name: String,
              username: { type: String, required: true, index: { unique: true }},
             password: { type: String, required: true, select: false },
             attendence: [{
                    month   :{type:String},
                    year    :{type:String},
                    day     :{ type:String},
                    attended:{type: Boolean}

            }]          
         });

我想在角度js表wiyh nodejs和mongodb中针对每个用户名显示每个日期(按月+年+日合并)的参与情况。在moongose。

2 个答案:

答案 0 :(得分:0)

希望这段代码能帮到你..

这是客户端角度js代码,用户可以使用日期和时间在数据库中插入。

    <form method="post" name="ravi" action="/">
  username:   <input type="text"  placeholder="username" name="user"  required><br/>
     date :<input type="number" placeholder="year"  name="year" min="2015" max="2017" required>
      <input type="number" placeholder="month"  name="month" min="1" max="12" required>
      <input type="number" placeholder="day" name="day" min="1" max="31" required><br/>
     Time:  <input type="number" placeholder="HH"  name="hour" min="0" max="23" required>
      <input type="number" placeholder="MM" name="minutes" min="0" max="59" required><br/>
            <input type="submit" >
    </form>

这是使用日期和时间在数据库中插入userdetaile的服务器端..

MongoClient.connect("mongodb://localhost:27017/bhawna", function(err, db) {
  if(err) { return console.dir(err); }
  var collection2 = db.collection('sadulehistory');


     var searchitem = (req.body.user).toUpperCase();
     console.log(searchitem);
     //var searchitem1 =(req.params.aa)
     //console.log(JSON.stringify(req.body.search));
     var yr = req.body.year;
     var moy = req.body.month;
     var dom = req.body.day;
     var hr = req.body.hour;
     var mnt = req.body.minutes;


     collection2.insert({
         'searchitem': searchitem,
         'Scheduledtime': (new Date(yr, (moy - 1), dom, hr, mnt)).toLocaleString(),
         'CurrentTime': (new Date()).toLocaleString()
     }, function(err, result) {});

     res.setHeader("Access-Control-Allow-Origin", "http://localhost:3000/")
     res.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
     res.setHeader("Access-Control-Allow-Headers", "Content-Type");
     res.redirect('back');
});

答案 1 :(得分:0)

你好bhawna这段代码必须帮助你...... 用户注意的客户端代码.index.html

<html>
  <head>
    <title>Attendence</title>
    <script src="angular.min.js"></script>
  </head>
  <body bgcolor="gray">

    <a href="Shistory.html">Attendence Listing</a>
    <div id="container" >
         <h1 align="center"> Attendence </h1>
<div align="center">
<form method="post" name="ravi" action="/">
 <input type="text"  placeholder="Name" name="Item"  required><br/>
 date :<input type="number" placeholder="year"  name="year" min="2015" max="2017" required>
  <input type="number" placeholder="month"  name="month" min="1" max="12" required>
  <input type="number" placeholder="day" name="day" min="1" max="31" required><br/>
 attended:<select name="attended">
  <option value="Present">Present</option>
  <option value="Absent">Absent</option>
    </select>
<br/>
        <input type="submit" value="Insert attendence" >
</form>
</div>

    </div>
  </body>
</html>

以及使用日期代码搜索用户的代码.Shistory.html

<html>
  <head>
    <title>Attendence</title>
    <script src="angular.min.js"></script>
  </head>
  <body bgcolor="gray">
    <a href="index.html">Attendence</a>
    <div id="container" >
<div align="center">
 <h1 align="center"> Attendence Listing </h1>
<form method="post" name="ravi1" action="/a">
 date :<input type="number" placeholder="year"  ng-model="year"  name="year" min="2015" max="2017" required>
  <input type="number" placeholder="month"  ng-model="month" name="month" min="1" max="12" required>
  <input type="number" placeholder="day"  ng-model="day" name="day" min="1" max="31" required><br/>
    <input type="submit" value="attendence">
</form>
</div>


    </div>
  </body>
</html> 

和您的服务器端代码.. server.js

var MongoClient = require('mongodb').MongoClient;
var connect = require('connect'),
 bodyParser = require('body-parser')
var ObjectId=require('mongodb').ObjectID;
var     BSON = require('mongodb').pure().BSON,
    express = require("express"),
    app = express(); 
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));


       // app.use(express.bodyParser());
        app.use(express.static(__dirname + '/angular'));
    // index.html and Sdistory.html in public folder
        app.use(express.static(__dirname + '/public'));
            app.get("/",function(req,res){
              res.sendFile(__dirname+'/index.html');
            });


        MongoClient.connect("mongodb://localhost:27017/bhawna", function(err, db) {
          if(err) { return console.dir(err); }
          var collection2 = db.collection('users');
        app.post('/a',function(req,res){
        //console.log("hello");

        var qqw=req.body.year;
        var qqw2=req.body.month;
        var qqw3=req.body.day;
                    collection2.find({'Scheduledtime':(new Date(qqw, (qqw2-1), qqw3)).toLocaleString()}).toArray(function(err,resu)
        {
            res.end( JSON.stringify(resu));
        console.log(resu);
                                        }
        );
        });
        app.post('/',function(req,res){

         var searchitem =(req.body.Item).toUpperCase();

                var  yr=req.body.year;
                var moy=req.body.month;
                var dom=req.body.day;
                var attended=req.body.attended;

         collection2.insert({'userd':searchitem,'Scheduledtime':(new Date(yr, (moy-1), dom)).toLocaleString(),'Attended':attended},function(err,result){});

        res.setHeader("Access-Control-Allow-Origin","http://localhost:8080/")
              re

s.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
           res.setHeader("Access-Control-Allow-Headers", "Content-Type");
    res.redirect('back');



      });  
    });
    app.listen(8080,function(){
      console.log("It's Started on PORT 8080");
    });