In Mongoose, how do you find an array of objects where an object has a specific key/value?

In the Mongoose model Person, there is an array of objects called matches.

matches looks like

            "$oid": "558ced061d35bd072e7b5825"
            "$oid": "558ced061d35bd072e7b58a0"
            "$oid": "558ced061d35bd072e7b58c6"

I want to create a query that finds a Person who has the ID in his matches array.

The problem is that the query for matches only seems to work for arrays of strings, like

models.Person.find({matches: 558ced061d35bd072e7b58c6})

but not for arrays of objects that contain string values.

Does anyone know how to accomplish this?


You can cast the string to ObjectId and use that in your query as follows:

var mongoose = require("mongoose"),
    person_id  = mongoose.Types.ObjectId("558ced061d35bd072e7b58c6");

models.Person.find({ "matches": person_id }).exec(callback);

Need Your Help

How to set values into custom DataGridBound column in WPF

c# .net wpf datagrid datepicker

Since there is no way to paste values into a DataGridTemplateColumn. I found some suggestions for creating my own column class derived from DataGridBoundColumn. The sample below adds a DatePicker t...

Is it OK to have multiple NSManagedObjectContext instances per thread?

iphone core-data nsmanagedobjectcontext

You have to have one per thread, but beyond that ... should you have more? Is it harmful to have more?