Suppose I have a Word model with this schema
var Word = new Schema({ name: { type: String, required: true }, disambiguation: String, partOfSpeech: { type: ObjectId, ref: "PartOfSpeech", required: true }, attributes: [{ type: ObjectId, ref: "Attribute"}], root: [{ type: ObjectId, ref: "Word"}], language: { type: ObjectId, ref: "Language", required: true } });
I want to execute a query that returns an object, with word names as keys with and values as arrays of documents containing words with the corresponding name.
As an example, here is a conclusion that I would like. Most fields are omitted for brevity.
{ stick: [{ _id: "5024216f6df57b2b68834079", partOfSpeech: "noun" }, { _id: "678451de6da54c2b68837345", partOfSpeech: "verb" }], dog: [{ _id: "47cc67093475061e3d95369d", partOfSpeech: "noun" }] }
This way, I can have random access to the list of words, so I do not need to iterate over it many times. Is there a built-in way to do this in mongoose?
source share