couchbaselabs / node-ottoman

Node.js ODM for Couchbase
https://ottomanjs.com/
Apache License 2.0
287 stars 98 forks source link

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

Closed ldoguin closed 1 year ago

ldoguin commented 1 year ago

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.

gsi-alejandro commented 1 year ago

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