【问题标题】:Mongo Query does not return anythingMongo 查询不返回任何内容
【发布时间】:2019-05-08 18:01:45
【问题描述】:

我正在尝试获取具有主题化学并符合给定用户时间表的所有组。现在查询没有返回任何东西。我相信存在语法错误。我不知道我做错了什么。

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}}
  ]
})

【问题讨论】:

  • 您看到的“语法错误”是什么?
  • 我没有看到只是我的怀疑
  • 任何输出?请编辑问题以包含输出。可能不需要更改此类数据或查询。

标签: javascript mongodb express mongoose


【解决方案1】:

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

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}}]}
        ]
    })

【讨论】:

    猜你喜欢
    • 2012-04-14
    • 1970-01-01
    • 2021-04-24
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多