bryan
bryan

Reputation: 9399

unknown operator "0" when doing "$elemMatch" in .find()

Issue

I have a pouchdb-express server I am using for some tests for a CouchDB database.

I have the following database with an item like so:

{
    _id: "12345",
    email: '[email protected]',
    companies: [{ id: 'company', uid: 'u0' }]
}

I then run the following command that works on my API hitting the CouchDB database but does not work when I try it on the PouchDB Express server.

.find({selector: { "companies": { "$elemMatch": {id: "company", uid: "u0"} } }})

I get the following error:

{ error: 'bad_request',   reason:  'unknown operator "0" - should be one of $eq, $lte, $lt, $gt, $gte, $exists, $ne, $in, $nin, $size, $mod, $regex, $elemMatch, $type, $allMatch or $all',   name: 'bad_request',   status: 400,   message:  'unknown operator "0" - should be one of $eq, $lte, $lt, $gt, $gte, $exists, $ne, $in, $nin, $size, $mod, $regex, $elemMatch, $type, $allMatch or $all' }

I also get the same exact error during the following query:

.find({
  limit:9999999,
  selector:{
    $or: [
      {$and: [{type:"something"},{uid:"u0"}] }, 
      {$and: [{type:"document"}]}
    ]
  }
})

I've also tried doing $eq almost exactly like the test suite does with still no dice.

Does anyone know why this would happen?

Info

Here is my package.json

"pouchdb-find": "^6.4.3", // tried 7.0.0 and still have the issue
"pouchdb-node": "^6.4.3",
"pouchdb-server": "^4.1.0",

Upvotes: 1

Views: 511

Answers (1)

Martin Bramwell
Martin Bramwell

Reputation: 2121

I have tinkered with this and discovered that for records like:

{
  "borough": "Brooklyn",
  "cuisine": "American ",
  "marks": [50, 60, 45, 43],
  "grades": [
    {
      "grade": "A",
      "score": 5
    },
    {
      "grade": "A",
      "score": 7
    },
    {
      "grade": "A",
      "score": 12
    },
    {
      "grade": "A",
      "score": 12
    }
  ],
  "name": "Riviera Caterer"
},

This simple selector will return the correct results :

{ selector: { '$elemMatch': { '$gte': 0, '$lt': 30 } } }

and, while this "composite (?)" selector, ignores mismatches, and returns all rows!! ...

{ selector: 'grades': { '$elemMatch': { 'score': 14 } } }

... this one bombs with the error you indicate :

{ selector: 'grades': { '$elemMatch': { "grade": "B" } } }

I suspect the "pouchdb-find" version of $elemMatch can only handle simple arrays, not arrays of objects.

Seems like a PR is required :-(

Upvotes: 1

Related Questions