什么应该是mongo查询

时间:2016-01-09 14:47:04

标签: node.js mongodb

下面是我收集的超过20,000,000份文件的文件。 我需要通过特定的zip找到文档,从这些文档中我需要从每个邮政地址(ADDR, CITY, STATE, ZIP, APT)中选择一条记录,其年龄值为18或更高。 结果需要限制为最终用户输入的数字。

{
    "_id" : ObjectId("55e86e98f493590878bb45d7"),
    "RecordID" : 84096380,
    "FN" : "Michael",
    "MI" : "",
    "LN" : "Horn",
    "NAME_PRE" : "MR",
    "ADDR" : "160 Yankee Camp Rd",
    "CITY" : "Telford",
    "ST" : "TN",
    "ZIP" : 37690,
    "APT" : "",
    "Z4" : 2200,
    "DPC" : 605,
    "CAR_RTE" : "R001",
    "WALK_SEQ" : 228,
    "LOT" : "0136A",
    "FIPS_ST" : 47,
    "FIPS_CTY" : 179,
    "LATITUDE" : 36.292787,
    "LONGITUDE" : -82.568171,
    "ADDR_TYP" : 1,
    "MSA" : 3660,
    "CBSA" : 27740,
    "ADDR_LINE" : 3,
    "DMA_SUPPR" : "",
    "GEO_MATCH" : 1,
    "CENS_TRACT" : 61900,
    "CENS_BLK_GRP" : 1,
    "CENS_BLK" : 17,
    "CENS_MED_HOME_VALUE" : 953,
    "CENS_MED_HH_INCOME" : 304,
    "CRA" : "",
    "Z4_TYP" : "S",
    "DSF_IND" : 1,
    "DPD_IND" : "N",
    "PHONE_FLAG" : "Y",
    "PHONE" : NumberLong("4237730233"),
    "TIME_ZN" : "E",
    "GENDER" : "M",
    "NEW_TO_BLD" : "",
    "SOURCES" : 19,
    "BASE_VER_DT" : 20101,
    "COMP_ID" : NumberLong("3769001836"),
    "IND_ID" : 1,
    "INF_HH_RANK" : 1,
    "HOME_OWNR_SRC" : "V",
    "DOB_YR" : 1975,
    "DOB_MON" : 7,
    "DOB_DAY" : 10,
    "EXACT_AGE" : 39,
    "AGE" : 39,
    "HH_INCOME" : "D"     
}

3 个答案:

答案 0 :(得分:1)

如果您使用的是mongoose,我们可以通过点(。)运算符链接操作。因为我看到你的所有需求都是有条件的,例如 -

Person.
  find({
  ZIP: "37690",
  ADDR : "",
  STATE : "", //so on
  AGE: { $gt: 18 }
  }).
  limit(10).
  exec(callback);

更多信息 - http://mongoosejs.com/docs/queries.html

答案 1 :(得分:1)

您需要使用聚合操作。

var pipeline = [
  {
    $match: {ZIP: 37690, AGE: {$gt: 18}}
  }, {
    $group: {
      _id: {ADDR: '$ADDR', CITY: '$CITY', STATE: '$STATE', ZIP: '$ZIP', APT: '$APT'},
      PHONE: {$first: '$PHONE'}
    }
  },
  {$limit: 10}
];
db.mycoll.aggregate(pipeline)

增强上述功能以投影结果中需要的任何字段

答案 2 :(得分:0)

我认为此查询可以解决您的问题。

Person.find({
      ZIP: "37690",
      AGE: { $gt: 18 }
  }).
  limit(50).
  exec(callback);