1

I have the data in MongoDB like following:

{

  _id: ObjectId('xxxxxxxxxxxx'), 
  data: {
   key1: value 1,
   key2: value 2,
   ...
   } 
 ...
}

I use Mongoose in Node.js app to connect to MongoDB.
I want to query all objects which the data field contains a certain string i.e: 'apple'

{
     data: {
        ...
        key3: 'I'd like to eat apple'
        ....
     }
}

How can I make this kind of query with Mongoose?

Colin Wang
  • 6,778
  • 5
  • 26
  • 42
  • 1
    Possible duplicate of [MongoDB Query Help - query on values of any key in a sub-object](http://stackoverflow.com/questions/19802502/mongodb-query-help-query-on-values-of-any-key-in-a-sub-object) – xShirase Dec 02 '16 at 15:30
  • You could definitely consider restructuring your schema as mentioned in the aforementioned link. One thing that also may help you with your problem is that you can use find like this... db.collection.find({'data.key3': /apple/}) and it will match your document. – dyouberg Dec 02 '16 at 15:41

0 Answers0