Mongo query (contains, and)

hi, i’m starting to learn mongodb and currently i’m stuck at a query :
Give the number of documents whose type list (properties.type_as_array) contains “geoserve” and “tectonic-summary ».
I’ve tried this query but the result is not correct:
db.collection.find({
$and:[{properties.type_as_array:
{$regex:”.geoserve.",".tectonic-summary."}
}]
});

Need your help. thanks

I believe what you want is the $all operator.

https://docs.mongodb.com/manual/reference/operator/query/all/

thank a lot i’ve been tried it like that:
db.earthquakes.find( {$and:{[ “properties.type_as_array”: { $all: [ [ “geoserve”, “tectonic-summary” ] ] } ]}} )
but still the same error

You don’t need the $and due to the fact that $all is an implicit $and

Try it like this:

db.earthquakes.find({
  'properties.type_as_array': {
    $all: ['geoserve', 'tectonic-summary']
  }
});

if i add count () at the end of the request i get 0 which is not normal. I have a JSON file that I import which contains a lot of data. So the result should not return 0.

db.earthquakes.find({
‘properties.type_as_array’: {
$all: [‘geoserve’, ‘tectonic-summary’]
}
}).count();

===> 0

{
“_id” : ObjectId(“5e08deca24e21f71a765f914”),
“type” : “Feature”,
“properties” : {
“mag” : 1.4,
“place” : “8km NW of Cobb, California”,
“time” : NumberLong(1370259941100),
“updated” : NumberLong(1370260390334),
“tz” : NumberInt(-420),
“url” : “http://earthquake.usgs.gov/earthquakes/eventpage/nc72001615”,
“detail” : “http://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/nc72001615.geojson”,
“felt” : null,
“cdi” : null,
“mmi” : null,
“alert” : null,
“status” : “AUTOMATIC”,
“tsunami” : null,
“sig” : NumberInt(30),
“net” : “nc”,
“code” : “72001615”,
“ids” : “,nc72001615,”,
“sources” : “,nc,”,
“types” : “,general-link,geoserve,nearby-cities,origin,phase-data,scitech-link,”,
“nst” : null,
“dmin” : 0.00898315,
“rms” : 0.07,
“gap” : 136.8,
“magType” : “Md”,
“type” : “earthquake”,
“iso_date” : ISODate(“2013-06-03T11:45:41.100+0000”),
“types_as_array” : [
“general-link”,
“geoserve”,
“nearby-cities”,
“origin”,
“phase-data”,
“scitech-link”
]
}