Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js mongoose: how to use the .in and .sort methods of a query?

I'm trying to wrap my head around mongoose, but I'm having a hard time finding any kind of documentation for some of the more advanced query options, specifically the .in and .sort methods. What's the syntax for sorting, for example, a Person by age?

db.model("Person").find().sort(???).all(function(people) { });

Then, let's say I want to find a Movie based on a genre, where a Movie can have many genres (in this case, an array of strings). Presumably, I'd use the .in function to accomplish that, but I'm not sure what the syntax would be. Or perhaps I don't have to use the .in method at all...? Either way, I'm lost.

db.model("Movie").find().in(???).all(function(movies) { });

Anyone have any ideas? Or even better, a link to some comprehensive documentation?

Thanks!
Chris

like image 986
Chris Harrington Avatar asked Dec 23 '10 23:12

Chris Harrington


People also ask

How do you use findById in Mongoose?

What is findById() in Mongoose? In MongoDB, all documents are unique because of the _id field or path that MongoDB uses to automatically create a new document. For this reason, finding a document is easy with Mongoose. To find a document using its _id field, we use the findById() function.

How do I join two collections in MongoDB using NodeJS?

Join Collections MongoDB is not a relational database, but you can perform a left outer join by using the $lookup stage. The $lookup stage lets you specify which collection you want to join with the current collection, and which fields that should match.

What does find () return in Mongoose?

The find() function is used to find particular data from the MongoDB database.

What is .select in Mongoose?

select() is a method of Mongoose that is used to select document fields that are to be returned in the query result. It is used to include or exclude document fields that are returned from a Mongoose query. The select() method performs what is called query projection.


1 Answers

Yeah, the mongoose documentation is lagging on some of these things, and unfortunately the solution is not very intuitive (comes with the territory when using something still going through rapid development and API changes on the way to version 1.0)

Meanwhile, this will do what you're looking for in terms of sorting:

db.model("Person").find().sort([['age','ascending']]).all(function(people) { });

As for the question about more complex nested relationships, if you haven't already, you may want to start with the excellent MongoDB documentation, specifically the articles on Schema Design, Advanced Queries and Dot Notation (reaching into objects). Knowing MongoDB inside and out should make navigating the murkier parts of mongoose a breeze.

Here's an example for finding movies by genre using $in:

db.model("Movie").find({ 'genres': { $in: ['Horror','Comedy'] } }).all(function(movies) { });
like image 95
Daniel Mendel Avatar answered Oct 13 '22 01:10

Daniel Mendel