couchbaselabs / node-ottoman

Node.js ODM for Couchbase

Home Page:https://ottomanjs.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The $in operator does not work as it does in Mongoose

ldoguin opened this issue · comments

It seems like the $in operator does not have the same behavior between Ottoman and Mongoose.

With Ottoman you have to write:

        query[`"${req.query.tag}"`] = { $in: { $field: 'tagList' } }        

With Mongoose you have to write:

    query.tagList = { $in: [req.query.tag] }

Basically the terms are inverted. Here is the resulting queries in both situations:

        query[`"${req.query.tag}"`] = { $in: { $field: 'tagList' } }   
        SELECT `Article`.* FROM `default`.`_default`.`Article` WHERE "test" IN tagList AND _type="Article" ORDER BY createdAt DESC LIMIT 10

        query.tagList = { $in: [req.query.tag] }     
        SELECT `Article`.* FROM `default`.`_default`.`Article` WHERE tagList IN ["test"] AND _type="Article" ORDER BY createdAt DESC LIMIT 10

Ideally we would be compatible with Mongoose, if not our documentation should explain the differences.

hi @ldoguin

You can use exactly the same syntax in Ottoman:

query.tagList = { $in: [req.query.tag] }

this will work.

On other hand, Ottoman queries are built using N1QL, and of course, Ottoman's goal is to make easier the way to build queries, but N1QL knowledge is required to use Couchbase full power and understand the queries.

"test" IN tagList : means "test" string exist in tagList field (the field can be a complex object, also check the WITHIN operator)
tagList IN ["test"]: means tagList field has "test" string (think in ["test", "test2"] like an enum)

For a more detailed answer please visit Couchbase's documentation IN operator section, you can find how IN operator works