$match的聚合查询适用于MongoDB Compass,但不适用于我的Node.js应用程序



我有一个Booking模型和一个Event模型。我正在尝试查询并检查是否有任何预订已经具有特定Event._idUser._id,以停止为该用户和事件创建重复的预订。聚合查询适用于MongoDB指南针,但是当我在Node.js应用程序中尝试查询时,它只会给我一个空数组

模型

预订

const BookingSchema = new Schema({
    amount: {
        type: Number,
        required: 'Please supply a number of people',
    },
    event: {
        type: mongoose.Schema.ObjectId,
        ref: 'Event',
        required: 'Must give an event!',
    },
    booker: {
        type: mongoose.Schema.ObjectId,
        ref: 'User',
        required: 'You must supply the booker!',
    },
    confirmed: {
        type: Boolean,
        default: false,
    },
});

事件

const eventSchema = new Schema(
    {
        title: {
            type: String,
            trim: true,
            required: 'You must add an event name!',
        },
        description: {
            type: String,
            trim: true,
        },
        slug: String,
        created: {
            type: Date,
            default: Date.now,
        },
        date: {
            type: Date,
            min: Date.now,
            required: 'Please enter a valid event Date!',
        },
        minCapacity: {
            type: Number,
            required: 'Please enter a correct min capacity for your event!',
        },
        maxCapacity: {
            type: Number,
            required: 'Please enter a correct max capacity for your event!',
        },
        price: Number,
        location: {
            type: {
                type: String,
                default: 'Point',
            },
            coordinates: [
                {
                    type: Number,
                    required: 'You must supply coords!',
                },
            ],
            address: {
                type: String,
                required: 'Please enter a valid address!',
            },
        },
        photo: String,
        author: {
            type: Schema.ObjectId,
            ref: 'User',
            required: 'You must supply an author!',
        },
        available: Boolean,
        // attendees: [User], you can do through virtuals
    },
    {
        toJSON: { virtuals: true },
        toObject: { virtuals: true },
    }
);
eventSchema.virtual('bookings', {
    ref: 'Booking', // what model is linked?
    localField: '_id', //what field on model
    foreignField: 'event', //which field on Booking?
});
module.exports = mongoose.model('Event', eventSchema);

查询

exports.createBooking = async (req, res) => {
    req.body.booker = req.user._id;
    req.body.event = req.params.id;
    const bookings = await Booking.aggregate(
        [
            {
                $match: {
                    event: req.params.id,
                },
            },
            { $count: 'bookings' },
        ],
    );
    return res.json(bookings);
};

提前谢谢你!如果您想要任何其他信息,请告诉我。

你必须将你的idString投射到ObjectId

const ObjectID = require('mongodb').ObjectID
[
  { "$match": {
    "event": ObjectId(req.params.id),
  }},
  { "$count": "bookings" },
]

最新更新