带有node.js的Mongodb TTL

时间:2016-03-28 19:27:03

标签: node.js mongodb mongoose

我正在努力用TTL方法从mongodb中删除文档。 它应该在最大1.10分钟后删除文档,但永远存在。

这是我的代码:

模特:

var mongo = require('mongoose'),
Schema = mongo.Schema;

var article = new Schema({
createdAt: { type: Date, expires: '10s'},
title: {type: String},
author: {type: String},
year: {type: String},
abstract: {type: String},
article: {type: String},
score: {type: String},
category: {type: String},
link1: {type: String},
link2: {type: String}
});

module.exports = mongo.mod

该应用:

var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');

var db = mongoose.connect('mongodb://localhost/sasapi');

var article = require('./models/article');

const port = 3000;
var app = express();

app.use(bodyParser.json());
var sasRouter = express.Router();

sasRouter.route('/articles')
  .post(function(req, res) {
var art = new article(req.body);

    art.save();
      res.send("Artikkelen \"" + art.title +"\" lagt til!");

 })
.get(function (req, res) {
    var filter = {};
    if (req.query.title){
        filter.title = req.query.title;
    }
    if(req.query.author){
        filter.author = req.query.author;
    }
     article.find(filter, function(err, articles){
     if(err){
     res.status(500).send(err);
     }else{
     if(!articles.length){
         res.send("Ingen treff på filtrering")
     }else {

         res.json(articles);
     }
     }
     });

});

1 个答案:

答案 0 :(得分:0)

您需要在该列上创建索引。

<%@ Master Language="VB" AutoEventWireup="false" CodeBehind="Main.master.vb" Inherits="Template.Main" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

<link href="Content/bootstrap.css" rel="stylesheet" />
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>  
        <script src="Scripts/jquery-2.2.1.js"></script>
            <script src="Scripts/bootstrap.js"></script>

<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
  <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
    <span class="sr-only">Toggle navigation</span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
  </button>
  <a class="navbar-brand" href="#">Brand</a>
</div>

<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
  <ul class="nav navbar-nav">
    <li class="active"><a href="google.com">Link <span class="sr-only">(current)</span></a></li>
    <li><a href="Redirect.aspx">Redirect</a></li>
    <li class="dropdown">
      <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
      <ul class="dropdown-menu">
        <li><a href="#">Action</a></li>
        <li><a href="#">Another action</a></li>
        <li><a href="#">Something else here</a></li>
        <li role="separator" class="divider"></li>
        <li><a href="#">Separated link</a></li>
        <li role="separator" class="divider"></li>
        <li><a href="#">One more separated link</a></li>
      </ul>
    </li>
  </ul>
  <form class="navbar-form navbar-left" role="search">
    <div class="form-group">
      <input type="text" class="form-control" placeholder="Search"/>
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
  </form>
  <ul class="nav navbar-nav navbar-right">
    <li><a href="#">Link</a></li>
    <li class="dropdown">
      <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
      <ul class="dropdown-menu">
        <li><a href="#">Action</a></li>
        <li><a href="#">Another action</a></li>
        <li><a href="#">Something else here</a></li>
        <li role="separator" class="divider"></li>
        <li><a href="#">Separated link</a></li>
      </ul>
    </li>
  </ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>

<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
                        <%-- Start <row> then <col> for pages --%>
                    </asp:ContentPlaceHolder>
<form id="form1" runat="server">
    <div>

        <div class="container">

            <%--  Holds the entire Grid--%>
            <div class="row">
                <%-- Row needed per columns--%>
                <div class="col-sm-12">

                </div>
            </div>
        </div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder3" runat="server">
        </asp:ContentPlaceHolder>
    </div>
</form>
</body>
</html>