【发布时间】:2021-01-20 22:54:36
【问题描述】:
我发现了这个与我类似的问题,但没有解释 Mongoose find query vs $match,我正在尝试做类似的事情。
我设法按今天的日期正确过滤,但我还不能按客户 ID 过滤。
这行得通:
var today = new Date(dateTime);
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 1!
var mmd = String(today.getMonth()).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
const filter = {
$match: {
$and: [
{ date: { $gt: new Date(Date.UTC(yyyy, mmd, dd)) } },
// { client: '5f78a00e97f9aa002aa7ec1c' },
{ status: 1 }
]
}
};
// the correct date is whit mmd not mm
const dateSent = new Date(Date.UTC(yyyy, mmd, dd));
console.log(dateSent);
const wpreservationDbagregate = await WorkplaceReservation.aggregate([filter]);
res.json(wpreservationDbagregate);
我收到此回复,旧文档已被正确过滤:
[
{
"_id": "5f7b93e89d1cb4600e8ce740",
"status": 1,
"workplace": 5,
"date": "2020-10-09T00:00:00.000Z",
"creator": "5f7b36b090b6e518210c6070",
"client": "5f78a00e97f9aa002aa7ec1c",
"userId": "5f7b36b090b6e518210c6070",
"dateCreated": "2020-10-05T21:45:12.229Z",
"__v": 0
},
{
"_id": "5f7b95699d1cb4600e8ce742",
"status": 1,
"workplace": 2,
"date": "2020-10-07T00:00:00.000Z",
"creator": "5f7b36b090b6e518210c6070",
"client": "5f78a00e97f9aa002aa7ec1c",
"userId": "5f7b36b090b6e518210c6070",
"dateCreated": "2020-10-05T21:51:37.219Z",
"__v": 0
}
]
但是当通过客户端 ID 进行过滤时它不起作用,即使使用字符串(它在过滤器常量中被注释掉)
我想我必须以某种方式解析它,但我可以找到方法......
这是架构:
import mongoose from 'mongoose';
const Schema = mongoose.Schema;
const workplaceReservationSchema = new Schema({
client: { type: Schema.Types.ObjectId, ref: 'user', required: [true, 'El espacio de trabajo debe ser reservado para algun Cliente'] },
workplace: { type: Number, required: [true, 'El espacio de trabajo es un campo obligatorio'] },
userId: { type: Schema.Types.ObjectId, ref: 'user', required: [true, 'El espacio de trabajo debe ser reservado para algun usuario'] },
creator: { type: Schema.Types.ObjectId, ref: 'user', required: [true, 'Loging incorrecto'] },
date: { type: Date, required: [true, 'La reserva debe tener una fecha'] },
dateModified: { type: Date },
dateCreated: { type: Date, default: Date.now },
status: { type: Number, default: 1 }
});
//Set unique compound indexes
workplaceReservationSchema.index({ workplace: 1, client: 1, date: 1, status: 1 }, { unique: true });
workplaceReservationSchema.index({ userId: 1, client: 1, date: 1, status: 1 }, { unique: true });
const WorkplaceReservation = mongoose.model('workplaceReservation', workplaceReservationSchema);
export default WorkplaceReservation;
package.json:
"dependencies": {
"@babel/cli": "^7.10.4",
"@babel/core": "^7.10.4",
"@babel/node": "^7.10.4",
"@babel/preset-env": "^7.10.4",
"bcrypt": "^4.0.1",
"connect-history-api-fallback": "^1.6.0",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.10.7",
"mongoose-unique-validator": "^2.0.3",
"morgan": "^1.10.0",
"underscore": "^1.10.2"
}
非常感谢!
【问题讨论】:
标签: json mongodb mongoose match aggregate