首页 > 解决方案 > 如何解决 MongoDB 查询括号错误?

问题描述

db.restaurants.find({ $or: [ { $and: [ {cuisine: {$ne: 'American'}}, {cuisine: {$ne: 'Chinese'}} ] }]}, {name: "/^Wil/"} ] }, {name: 1, cuisine:1})

错误:

uncaught exception: SyntaxError: missing ) after argument list :

这些长查询有很多括号,我无法正确匹配它们。是否有我们应该用于编写这些查询的特定工具/IDE?

标签: databasemongodbnosql

解决方案


试试这个:

db.restaurants.find({
  $or: [
    {
      $and: [
        {
          cuisine: {
            $ne: "American"
          }
        },
        {
          cuisine: {
            $ne: "Chinese"
          }
        }
      ]
    },
    {
      name: /^Wil/
    }
  ]
},
{
  name: 1,
  cuisine: 1
})

Mongo游乐场

对于非常简单的查询,请使用MongoPlayground
对于更复杂的查询,我通常使用Robo3t / Studio3t.


推荐阅读