I am facing an issue, actually I have mongoose module
我正面临一个问题,实际上我有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);
I want to query all documents of msgTiming 5 minutes from now, but I can't get it.
我想从现在起5分钟后查询msgTiming的所有文件,但我无法得到它。
My query is
我的疑问是
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);
}
});
});
I don't know how to find all records of 5 minutes from now and response array should be on msgTiming arrangement
我不知道如何找到从现在起5分钟的所有记录,响应数组应该在msgTiming安排上
Please help me, I have been facing this problem for the past 2 days; any suggestions will be appreciated
请帮助我,过去2天我一直面临这个问题;任何建议将不胜感激
Thanks in advance
提前致谢
2 个解决方案
#1
0
You can do like this
你可以这样做
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
you have to use moment npm
你必须使用时刻npm
first change your msgTiming : { type : Date},
to msgTiming : { type : Number}
in Visitor Model Schema .
首先将访问者模型模式中的msgTiming:{type:Date}更改为msgTiming:{type:Number}。
when you inserting a recored, set msgTiming: moment().unix()
in your Object.
当你插入一个recored时,在你的Object中设置msgTiming:moment()。unix()。
it will store current unix timestamp in seconds
like 1318874398
它将以秒为单位存储当前的unix时间戳,如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);
});
});
#1
0
You can do like this
你可以这样做
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
you have to use moment npm
你必须使用时刻npm
first change your msgTiming : { type : Date},
to msgTiming : { type : Number}
in Visitor Model Schema .
首先将访问者模型模式中的msgTiming:{type:Date}更改为msgTiming:{type:Number}。
when you inserting a recored, set msgTiming: moment().unix()
in your Object.
当你插入一个recored时,在你的Object中设置msgTiming:moment()。unix()。
it will store current unix timestamp in seconds
like 1318874398
它将以秒为单位存储当前的unix时间戳,如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);
});
});