MongoDB喜欢查询

时间:2013-09-26 07:51:38

标签: regex mongodb

我的馆藏包含以下文件结构

{ "_id" : ObjectId("5242c91044aef624318eaf7d"), "memoryUtilization" : "10",    "cpuUtilization" : "4", "hostId" : "10.9.1.3", "timestamp" : 1379570550000, "device_type" :  "snmp" }
{ "_id" : ObjectId("5242c91044aef624318eaf7e"), "memoryUtilization" : "31", "cpuUtilization" : "0", "hostId" : "10.9.1.14", "timestamp" : 1379570550000, "device_type" : "snmp" }
{ "_id" : ObjectId("5242c91044aef624318eaf7f"), "memoryUtilization" : "61", "cpuUtilization" : "12", "hostId" : "10.9.10.40", "timestamp" : 1379570550000, "device_type" : "snmp" }
{ "_id" : ObjectId("5242c91044aef624318eaf80"), "memoryUtilization" : "61", "cpuUtilization" : "12", "hostId" : "10.9.17.41", "timestamp" : 1379570550000, "device_type" : "snmp" }
{ "_id" : ObjectId("5242c91044aef624318eaf81"), "memoryUtilization" : "55", "cpuUtilization" : "10", "hostId" : "10.9.1.42", "timestamp" : 1379570550000, "device_type" : "snmp" }

现在我想要所有的文件,其hostId以“10.9.1”开头,所以我的结果应该如下所示

{ "_id" : ObjectId("5242c91044aef624318eaf7d"), "memoryUtilization" : "10",    "cpuUtilization" : "4", "hostId" : "10.9.1.3", "timestamp" : 1379570550000, "device_type" :  "snmp" }
{ "_id" : ObjectId("5242c91044aef624318eaf7e"), "memoryUtilization" : "31", "cpuUtilization" : "0", "hostId" : "10.9.1.14", "timestamp" : 1379570550000, "device_type" : "snmp" }
{ "_id" : ObjectId("5242c91044aef624318eaf81"), "memoryUtilization" : "55", "cpuUtilization" : "10", "hostId" : "10.9.1.42", "timestamp" : 1379570550000, "device_type" : "snmp" }

因为我写了以下查询,但它不起作用

db.collectionsname.find({"hostId":/10.9.1./})

它显示了包含hostId的所有结果,如10.9.1.42,10.9.10.40等所以任何人都知道我应该如何编写查询?

1 个答案:

答案 0 :(得分:1)

使用db.collectionsname.find({"hostId":/^10\.9\.1\./})(评论后编辑)

.是正则表达式中的一个特殊字符,它匹配每个字符,通常除了换行符之外(但这里有一些与语言相关的差异)。