我正面临一个问题,实际上我有mongoose模块
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var saveVisitorsDetails = new Schema({
userId : { type: String, unique: true, dropDups: true},
visitorName : { type: String},
visitorMobileNumber : { type: Number},
expireStatus : { type : Number},
clientUnreadMessageCount : { type : Number},
adminUnreadMessageCount : { type : Number},
visibleVisitorChat : { type : Boolean},
visibleAdminChat : { type : Boolean},
displayAdminChat : { type : Boolean},
activeChat : { type : Boolean},
adminId : { type : String},
visitorMsg : { type : String},
msgTiming : { type : Date},
showVisitorCross : { type : Boolean},
showVisitorMinimize : { type : Boolean},
newMessageForAdmin : { type : Boolean},
expireUserForAdmin : { type : Boolean},
visitorLocationObject : { type : Object }
});
module.exports = mongoose.model('Visitor',saveVisitorsDetails);
我想从现在起5分钟后查询msgTiming的所有文档,但我无法得到它。
我的查询是
var express = require('express');
var app = express();
var Visitor = require('./models/visitor');
app.post('/loadVisitorDetails', function(req, res){
Visitor.collection.find({adminId : req.body.adminId ,expireStatus : 0}).sort({msgTiming : -1}).toArray(function(err, result){
if(err){
console.log(err);
}else{
var obj = {
rslt : result,
id : req.body.adminId
}
res.send(obj);
}
});
});
我不知道如何查找从现在起5分钟内的所有记录,响应数组应该在msgTiming安排上
请帮助我,过去2天我一直面临这个问题;任何建议将不胜感激
提前致谢
答案 0 :(得分:1)
Manjeet的回答很不错,应该在after.getMinutes-5而不是+上做一个小小的修正。
var express = require('express');
var app = express();
var Visitor = require('./models/visitor');
app.post('/loadVisitorDetails', function (req, res) {
let now = new Date();
let after = new Date();
after.setMinutes(after.getMinutes() - 5);
let critria = {
$and: [{
adminId: req.body.adminId
}, {
expireStatus: 0
}, {
msgTiming: {
$gte: now,
$lte: after
}
}]
};
let projection = {}
let options = { sort:{ msgTiming : -1 }}
Visitor.collection.find(critria,projection,options,function(err,result){
if(err){
console.log(err)
} else {
console.log(result)
}
})
});
答案 1 :(得分:0)
你可以这样做
var express = require('express');
var app = express();
var Visitor = require('./models/visitor');
app.post('/loadVisitorDetails', function (req, res) {
let now = new Date();
let after = new Date();
after.setMinutes(after.getMinutes() + 5);
let critria = {
$and: [{
adminId: req.body.adminId
}, {
expireStatus: 0
}, {
msgTiming: {
$gte: now,
$lte: after
}
}]
};
let projection = {}
let options = { sort:{ msgTiming : -1 }}
Visitor.collection.find(critria,projection,options,function(err,result){
if(err){
console.log(err)
} else {
console.log(result)
}
})
});
答案 2 :(得分:0)
你必须使用moment npm
首先将访问者模型架构中的msgTiming : { type : Date},
更改为msgTiming : { type : Number}
。
当您插入已审核时,请在对象中设置msgTiming: moment().unix()
。
它将存储current unix timestamp in seconds
,如1318874398
app.post('/loadVisitorDetails', function(req, res){
var currenttime= moment().unix();
var 5minbefore=currenttime-300; // (5 * 60)
Visitor.collection.find({adminId : req.body.adminId,expireStatus : 0,msgTiming:{$gt: 5minbefore, $lte : currenttime}},
function(err, result){
console.log(result);
});
});