Get only string value of a field in mongoDB

时间:2016-08-31 17:46:58

标签: mongodb

I would like to get all documents with a string for the field 'readings'. So in this example:

{
   "_id": 1,
   "readings": "string"
},
{
   "_id": 2,
   "readings": [
      "string"
   ]
}

... I need to get document 1 as a result, NOT document 2.

I tried this

db.collection.find( { "readings" : { $type: "string" } } )

but this will give me document 2

1 个答案:

答案 0 :(得分:1)

Try this now:

db.collection.find( { "readings" : { $type : "string" }, "readings.0": { "$exists": false } } );

Reference: MongoDB Match an array with $type?