Domanda

Sto cercando di avvolgere la mia testa intorno mangusta, ma sto avendo difficoltà a trovare qualsiasi tipo di documentazione per alcune delle opzioni di query più avanzate, in particolare i .in e .Sort metodi. Qual è la sintassi per l'ordinamento, per esempio, una persona per età?

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

Quindi, diciamo che voglio trovare un film basato su un genere, in cui un film può avere molti generi (in questo caso, un array di stringhe). Presumibilmente, mi piacerebbe utilizzare la funzione .in per realizzare questo, ma non sono sicuro di quello che la sintassi sarebbe. O forse non c'è bisogno di utilizzare il metodo .in a tutti ...? Ad ogni modo, mi sono perso.

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

Qualcuno ha qualche idea? O meglio ancora, un link ad una documentazione completa?

Grazie!
Chris

È stato utile?

Soluzione

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) { });

Altri suggerimenti

Daniel's answer didn't work for me. I believe the api has changed since 1.0.14 (http://groups.google.com/group/mongoose-orm/browse_thread/thread/cc77914021940e73/34fc6f45938a2242?lnk=raot)

This is what worked for me:

db.model("Person")
  .find()
  .sort('age', 1)
  .execFind(function(err, people) { 
 });
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top