Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all the values that contains part of a string using mongoose find?

I have the following problem retrieving data from MongoDB using mongoose.

Here is my Schema:

const BookSchema = new Schema(     {         _id:Number,         title:String,         authors:[String],         subjects:[String]        } ); 

as you can see i have 2 arrays embedded in the object, let's say the content of the authors can be something like this: authors:["Alex Ferguson", "Didier Drogba", "Cristiano Ronaldo", "Alex"]
what I'm trying to achieve is get all the Alex in the array.

So far, I've been able to get the values if they match the value completely. However if I try to get the ones containing Alex the answer is always [].

What I want to know is how I can do this using find() without performing a map-reduce to create a view or a collection and then applying find() over that.

The code here works for exact matches

Book.find( {authors:req.query.q} , function(errs, books){             if(errs){                 res.send(errs);              }              res.json(books);         }); 

I tried some things but no luck {authors:{$elemMatch:req.query.q}} {authors:{$in:[req.query.q]}}

This one gives me an error and on top of that says is very inefficient in another post I found here. {$where:this.authors.indexOf(req.query.q) != -1}

and I also tried {authors:{$regex:"./value/i"}}

The map-reduce works fine, I need to make it work using the other approach to see which one is better?

Any help is greatly appreciated. I'm sure this is easy, but I'm new with NodeJS and Mongo and I haven't been able to figure it out on my own.

like image 279
Dyan Avatar asked Nov 08 '14 06:11

Dyan


People also ask

What is $in in mongoose?

The value of the $in operator is an array that contains few values. The document will be matched where the value of the breed field matches any one of the values inside the array.

What does find do in mongoose?

Mongoose | findOne() Function The findOne() function is used to find one document according to the condition. If multiple documents match the condition, then it returns the first document satisfying the condition.

What does find function return in mongoose?

find() function returns an instance of Mongoose's Query class. The Query class represents a raw CRUD operation that you may send to MongoDB. It provides a chainable interface for building up more sophisticated queries. You don't instantiate a Query directly, Customer.

What is __ V 0 in mongoose?

This is __v field that is only generated when a document(s) is inserted through mongoose. The __v field is called the version key. It describes the internal revision of a document. This __v field is used to track the revisions of a document. By default, its value is zero.


1 Answers

You almost answered this yourself in your tags. MongoDB has a $regex operator which allows a regular expression to be submitted as a query. So you query for strings containing "Alex" you do this:

Books.find(     { "authors": { "$regex": "Alex", "$options": "i" } },     function(err,docs) {      }  ); 

You can also do this:

Books.find(     { "authors": /Alex/i },      function(err,docs) {       } ); 

Both are valid and different to how you tried in the correct supported syntax as shown in the documentation.

But of course if you are actually asking "how to get the 'array' results only for those that match 'Alex' somewhere in the string?" then this is a bit different.

Complex matching for more than one array element is the domain of the aggregation framework ( or possibly mapReduce, but that is much slower ), where you need to "filter" the array content.

You start of much the same. The key here is to $unwind to "de-normalize" the array content in order to be alble to "filter" properly as individual documents. Then re-construct the array with the "matching" documents.

Books.aggregate(     [         // Match first to reduce documents to those where the array contains the match         { "$match": {             "authors": { "$regex": "Alex", "$options": i }         }},          // Unwind to "de-normalize" the document per array element         { "$unwind": "$authors" },          // Now filter those document for the elements that match         { "$match": {             "authors": { "$regex": "Alex", "$options": i }         }},          // Group back as an array with only the matching elements         { "$group": {             "_id": "$_id",             "title": { "$first": "$title" },             "authors": { "$push": "$authors" },             "subjects": { "$first": "$subjects" }         }}     ],     function(err,results) {      } ) 
like image 59
Neil Lunn Avatar answered Nov 03 '22 09:11

Neil Lunn