简体   繁体   中英

MongoDB & PHP - Regex find query in nested array

I have data like so:

{
   "_id": ObjectId("4fc3abc9751c38c811000002"),
   "timeStamp": ISODate("2012-05-28T16: 45: 58.0Z"),
   "loadTime": 2.3101460933685,
   "description": {
     "0": {
       "level": NumberInt(0),
       "description": "Some description"
    }
  },

How do I find() the "level" data using the array of 0-8 like in my query below?

My query looks like this:

find(Array ( [description] => Array ( [0] => Array ( [level] => Array ( [$in] => Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 ) ) ) ) ) )

This should do the trick:

->find( array( 'description.0.level' => array( '$in' => range( 0, 8 ) ) ) );

However, I don't quite see why you have a "0" key there. The document really should look like:

{
  "_id": ObjectId("4fc3abc9751c38c811000002"),
  "timeStamp": ISODate("2012-05-28T16: 45: 58.0Z"),
  "loadTime": 2.3101460933685,
  "description": [
    {
      "level": 0,
      "description": "Some description"
    }
  ],
}

In which case you can find the "level" like:

->find( array( 'description.level' => array( '$in' => range( 0, 8 ) ) ) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM