CarCrazyBen
CarCrazyBen

Reputation: 1136

how to exclude certain documents from Cloudant (or Mango) query?

I imported the IMDB movies-demo database to my CouchDB instance and am am learning about Mango by utilizing some Cloudant tutorials and references.

I wrote this query to identify documents wherein the actor's name is like "bruce". Now I want to modify this to exclude certain documents from the result set. For example Movie_name <> 'Monster'.

Is this possible and, if so, can anyone help me figure out how to do this?

{"selector":
  {"_id":
    {"$gt": null},
    "Person_name": {"$regex": "(?i)bruce"}
  }
}

Upvotes: 0

Views: 1331

Answers (1)

Glynn Bird
Glynn Bird

Reputation: 5637

There is $ne operator that can be used to match documents that are "not equal to" a given value.

{
  "selector":{
     "_id": {"$gt": null},
     "Person_name": {"$regex": "(?i)bruce"},
     "Movie_name": { "$ne": "Monster" }
  }
}

A word of caution: the use of $regex in a query is not a recipe for great performance because it inevitable forces CouchDB to do a manual scan of all the documents in the database, rather than taking advantage of a pre-built index. Similarly, the $ne operator has similar performance characteristics.

The recommended practice with a Cloudant database (a hosted CouchDB service) is to create a type=text index on the fields you are querying. This index will allow you to use the $text operator in your queries in place of your regular expression, which should be much more efficient.

Upvotes: 2

Related Questions