mongodb按日期查询返回错误结果

时间:2018-10-31 20:00:15

标签: javascript node.js mongodb express mongoose

我正在尝试按日期降序查询结果。我的应用程序中的日期变量称为approvaldate

myroute.js

'use strict';
var express = require('express');
var Ad = require('../models/ad.model.js');
var moment = require('moment');
var router = express.Router();

/* GET home page. */
router.get('/', function (req, res) {


    Ad.find({ adstatus: 'approved' }).sort({ approvaldate: -1 }).exec(function (err, approvedads) {
        if (err) {
            console.log(err);
        }

        else {

            approvedads.adsince = moment(approvedads.creationdate).fromNow();
            console.log(approvedads);
            res.render('index', { approvedads: approvedads});
        }


    });



});

module.exports = router;

下面是我在console.log(approvedads);被执行后得到的结果。请注意,返回的结果是如何在ad上加上heading的{​​{1}}和approvaldate并在[ { adstatus: 'approved', imgdest: [ 'img1.jpg', 'img2.jpg', 'img1.jpg', 'img2.jpg' ], _id: 5bd5f3d2ffc77735b4431db6, heading: 'This is so cool man!', metal: '22 Carat', weight: 233, unit: 'kilos', opdesc: 'LOL I don\'t know how to code tho!', userobjid: 5bccfb7515bc6d0c6872ed91, username: 'mesam', deliverydate: '', creationdate: 'Sun Oct 28 2018 21:37:22 GMT+0400 (Arabian Standard Time)', displaydate: '28 October 2018', approvaldate: 'Sun Oct 28 2018 21:37:32 GMT+0400 (Arabian Standard Time)', __v: 0 }, { adstatus: 'approved', imgdest: [ 'http://localhost:3000/images/5bd5f071ffc77735b4431db5/ img1.jpg', 'http://localhost:3000/images/5bd5f071ffc77735b4431db5/ img2.jpg' ], _id: 5bd5f071ffc77735b4431db5, heading: 'adwqd', metal: '22 Carat', weight: 3, unit: 'kilos', opdesc: 'asd', userobjid: 5bccfb7515bc6d0c6872ed91, username: 'mesam', deliverydate: '', creationdate: 'Sun Oct 28 2018 21:22:57 GMT+0400 (Arabian Standard Time)', displaydate: '28 October 2018', approvaldate: 'Sun Oct 28 2018 21:23:05 GMT+0400 (Arabian Standard Time)', __v: 0 }, { adstatus: 'approved', imgdest: [ 'img1.jpg', 'img2.jpg' ], _id: 5bd5e81209824946742fdbd7, heading: 'wqd', metal: '21 Carat', weight: 3, unit: 'kilos', opdesc: 'asd', userobjid: 5bccfb7515bc6d0c6872ed91, username: 'mesam', deliverydate: '', creationdate: 'Sun Oct 28 2018 20:47:14 GMT+0400 (Arabian Standard Time)', displaydate: '28 October 2018', approvaldate: 'Sun Oct 28 2018 20:47:37 GMT+0400 (Arabian Standard Time)', __v: 0 }, { adstatus: 'approved', imgdest: [ '/Users/mesam/source/repos/JewelryMP/JewelryMP/public/images/5bd4cb3c7b7563425c2a48e8/img.jpg' ], _id: 5bd4cb3c7b7563425c2a48e8, heading: 'MAN THIS IS NOW GOD WORKING', metal: '21 Carat', weight: 323, unit: 'kilos', opdesc: 'HAHAHA NICE', userobjid: 5bccfb7515bc6d0c6872ed91, username: 'mesam', deliverydate: '2018-10-31', creationdate: 'Sun Oct 28 2018 00:31:56 GMT+0400 (Arabian Standard Time)', displaydate: '27 October 2018', approvaldate: 'Sun Oct 28 2018 00:32:02 GMT+0400 (Arabian Standard Time)', __v: 0 }, { adstatus: 'approved', imgdest: [ '/Users/mesam/source/repos/JewelryMP/JewelryMP/public/images/5bd4c9db7b7563425c2a48e7/img.jpg' ], _id: 5bd4c9db7b7563425c2a48e7, heading: 'sdf', metal: '21 Carat', weight: 7876, unit: 'tons', opdesc: 'efwef', userobjid: 5bccfb7515bc6d0c6872ed91, username: 'mesam', deliverydate: '', creationdate: 'Sun Oct 28 2018 00:26:03 GMT+0400 (Arabian Standard Time)', displaydate: '27 October 2018', approvaldate: 'Sun Oct 28 2018 00:27:30 GMT+0400 (Arabian Standard Time)', __v: 0 }, { adstatus: 'approved', imgdest: [ '/Users/mesam/source/repos/JewelryMP/JewelryMP/uploads/5bd48e65d600893e9857f115/img.jpg' ], _id: 5bd48e65d600893e9857f115, heading: 'v', metal: '21 Carat', weight: 34, unit: 'grams', opdesc: 'das', userobjid: 5bccfb7515bc6d0c6872ed91, username: 'mesam', deliverydate: '', creationdate: 'Sat Oct 27 2018 20:12:21 GMT+0400 (Arabian Standard Time)', displaydate: '27 October 2018', approvaldate: 'Sat Oct 27 2018 20:16:17 GMT+0400 (Arabian Standard Time)', __v: 0 }, { adstatus: 'approved', imgdest: [], _id: 5bd63787fde06d407c375c4a, heading: 'MAN WE DID IT!', metal: '21 Carat', weight: 32, unit: 'kilos', opdesc: 'STILL NO IDEA HOW LOL!', userobjid: 5bccfb7515bc6d0c6872ed91, username: 'mesam', deliverydate: '2018-10-31', creationdate: 'Mon Oct 29 2018 02:26:15 GMT+0400 (Arabian Standard Time)', displaydate: '28 October 2018', approvaldate: 'Mon Oct 29 2018 02:26:50 GMT+0400 (Arabian Standard Time)', __v: 0 }, { adstatus: 'approved', imgdest: [], _id: 5bccfb9315bc6d0c6872ed92, heading: 'Man my DB could be messed up!', metal: '18 Carat', weight: 2, unit: 'grams', opdesc: 'Has it been messed up?', userobjid: 5bccfb7515bc6d0c6872ed91, username: 'mesam', deliverydate: '', creationdate: 'Mon Oct 22 2018 02:20:03 GMT+0400 (Arabian Standard Time)', displaydate: '21 October 2018', approvaldate: 'Mon Oct 22 2018 17:48:17 GMT+0400 (Arabian Standard Time)', __v: 0 }, { adstatus: 'approved', imgdest: [], _id: 5bccfbb115bc6d0c6872ed93, heading: 'This is the next post.', metal: '21 Carat', weight: 2, unit: 'grams', opdesc: 'Hopefully it works.', userobjid: 5bccfb7515bc6d0c6872ed91, username: 'mesam', deliverydate: '', creationdate: 'Mon Oct 22 2018 02:20:33 GMT+0400 (Arabian Standard Time)', displaydate: '21 October 2018', approvaldate: 'Mon Oct 22 2018 17:48:15 GMT+0400 (Arabian Standard Time)', __v: 0 }, { adstatus: 'approved', imgdest: [], _id: 5bccfbe615bc6d0c6872ed94, heading: 'So could we say large amount of DB', metal: '21 Carat', weight: 23, unit: 'kilos', opdesc: 'IS MESSING UP IT\'S SORT FUNCTION?', userobjid: 5bccfb7515bc6d0c6872ed91, username: 'mesam', deliverydate: '', creationdate: 'Mon Oct 22 2018 02:21:26 GMT+0400 (Arabian Standard Time)', displaydate: '21 October 2018', approvaldate: 'Mon Oct 22 2018 17:48:14 GMT+0400 (Arabian Standard Time)', __v: 0 }, adsince: 'a few seconds ago' ] Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info. Arguments: [0] _isAMomentObject: true, _isUTC: false, _useUTC: false, _l: undefined, _i: Sun Oct 28 2018 21:37:32 GMT+0400 (Arabian Standard Time), _f: undefined, _strict: undefined, _locale: [object Object] Error at Function.createFromInputFallback (C:\Users\mesam\source\repos\JewelryMP\JewelryMP\node_modules\moment\moment.js:320:98) at configFromString (C:\Users\mesam\source\repos\JewelryMP\JewelryMP\node_modules\moment\moment.js:2368:15) at configFromInput (C:\Users\mesam\source\repos\JewelryMP\JewelryMP\node_modules\moment\moment.js:2594:13) at prepareConfig (C:\Users\mesam\source\repos\JewelryMP\JewelryMP\node_modules\moment\moment.js:2577:13) at createFromConfig (C:\Users\mesam\source\repos\JewelryMP\JewelryMP\node_modules\moment\moment.js:2544:44) at createLocalOrUTC (C:\Users\mesam\source\repos\JewelryMP\JewelryMP\node_modules\moment\moment.js:2631:16) at createLocal (C:\Users\mesam\source\repos\JewelryMP\JewelryMP\node_modules\moment\moment.js:2635:16) at hooks (C:\Users\mesam\source\repos\JewelryMP\JewelryMP\node_modules\moment\moment.js:12:29) at model.dateHelper (C:\Users\mesam\source\repos\JewelryMP\JewelryMP\node_modules\helper-date\index.js:48:15) at eval (eval at createFunctionContext (C:\Users\mesam\source\repos\JewelryMP\JewelryMP\node_modules\handlebars\dist\cjs\handlebars\compiler\javascript-compiler.js:254:23), <anonymous>:6:134) at prog (C:\Users\mesam\source\repos\JewelryMP\JewelryMP\node_modules\handlebars\dist\cjs\handlebars\runtime.js:221:12) at execIteration (C:\Users\mesam\source\repos\JewelryMP\JewelryMP\node_modules\handlebars\dist\cjs\handlebars\helpers\each.js:51:19) at Object.<anonymous> (C:\Users\mesam\source\repos\JewelryMP\JewelryMP\node_modules\handlebars\dist\cjs\handlebars\helpers\each.js:61:13) at Object.eval (eval at createFunctionContext (C:\Users\mesam\source\repos\JewelryMP\JewelryMP\node_modules\handlebars\dist\cjs\handlebars\compiler\javascript-compiler.js:254:23), <anonymous>:6:31) at main (C:\Users\mesam\source\repos\JewelryMP\JewelryMP\node_modules\handlebars\dist\cjs\handlebars\runtime.js:175:32) at ret (C:\Users\mesam\source\repos\JewelryMP\JewelryMP\node_modules\handlebars\dist\cjs\handlebars\runtime.js:178:12) 星期一10月29日发布的。为什么这种方法坏了,我却无法解决。

批准的广告的输出

<div class="container">
    <div class="row">
        <div class="col-md-8 mx-auto">

            {{#each approvedads }}

            <div class="list-group list-group-flush ">

                <div href="#" class="list-group-item flex-column align-items-start borderless indexka">

                    <div class="d-flex h-15 d-inline-block justify-content-between">

                        {{#gt (moment this.approvaldate diff=moment) -21600000}}
                        <h6><span class="badge badge-info">New </span></h6>
                        {{/gt}}
                        <a href="/posts/{{this._id}}" class="mb-1 custom mr-auto">           {{this.heading}}</a>
                        <small class="navbar-text smalldate text-muted">{{moment this.approvaldate fromNow=null }}</small>


                        <!--<small></small>-->
                    </div>
                    <!--<p class="mb-1"> </p>-->
                    <!--<small style="font-style:italic">by</small>-->
                </div>


            </div>
            <br/>
            {{/each}}





        </div>




    </div>



</div>

index.handlebars

select ref
from actions
where status in ('new', 'done')
group by ref
having min(status) = max(status) and min(status) = 'new';

0 个答案:

没有答案