Mongo Query不返回任何内容

时间:2019-05-08 18:01:46

标签: javascript mongodb express mongoose

我正在尝试使所有具有主题化学性质并适合给定用户时间表的组。现在查询不返回任何内容。我相信有语法错误。我不知道我在做什么错。

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const GroupSchema = new Schema({
  owner: {type: mongoose.Schema.ObjectId, ref: 'users'},
  subject: String,
  date: {type: Date, default: Date.now},
  mondayStart: Number,
  mondayEnd: Number,
  tuesdayStart: Number,
  tuesdayEnd: Number,
  wednesdayStart: Number,
  wednesdayEnd: Number
});

module.exports = Group = mongoose.model('groups', GroupSchema);


let query = Group.find({
  subject: '/chemistry/',
  $or: [
    {$and: [ {mondayStart: {gte: 6}}, {mondayEnd:  {lte: 8}}] },
    {$and: [ {tuesdayStart: {gte: 9}}, {tuesdayEnd: {lte:13}}] },
    {$and: [ {wednesdayStart: {gte: 9}}, {wednesdayEnd: {lte: 12}}
  ]
})

1 个答案:

答案 0 :(得分:0)

您需要更改查询。下面给出的查询正在工作的示例:

let query = Group.find({
        subject: {$regex :'chemistry'},
      $or: [
        {$and: [ {mondayStart: {$gte: 6}}, {mondayEnd:  {$lte: 8}}] },
        {$and: [ {tuesdayStart: {$gte: 9}}, {tuesdayEnd: {$lte:13}}] },
        {$and: [ {wednesdayStart: {$gte: 9}}, {wednesdayEnd: {$lte: 12}}]}
        ]
    })