mongooseJS返回集合中的ID数

时间:2016-12-29 03:10:47

标签: javascript mongodb mongoose

我在Javascript中使用mongooseJS作为mongoDB驱动程序。我可以使用以下代码返回数据库中的字段数:

app.get('/total/', (req, res) => {
  var id = req.params.id;
  boxmac.count().then((macdb) => {
    res.send({macdb});

  }, (e) => {
    res.status(404).send(e)
    console.log(e);

  });
});

当我用节点加载文件并查询生成的链接时,我得到了

{
  "macdb": 2108
}

但是我试图返回集合中唯一ID的数量(应该是168-170)。我已经尝试将ID放在不同的位置(例如count({id})。然后,却无法获得ID的数量。

这是我的模特:

var boxmac = mongoose.model('macdbs', {
  ProductName: {
    type: String,
    required: true,
    minlength: 1,
    trim: true
  },
  OriginCountry: {
    type: String,
    default: false
  },

  StoreBrand: {
    type: String,
    default: null
  },

  Type: {
    type: String,
    default: null,
    required: true
  },
  Pasta: {
    type: String,
    default: null,
    required: false
  },
  ADC: {
    type: String,
    default: null,
    required: false
  },
  PastaType: {
    type: String,
    default: null,
    required: false
  },
  Org: {
    type: String,
    default: 'N',
    required: false
  },
  Veg: {
    type: String,
    default: 'N',
    required: false
  },
  Mic: {
    type: String,
    default: null,
    required: false
  },
  Exp: {
    type: String,
    default: null,
    required: false
  },
  PriceRaw: {
    type: String,
    default: null,
    required: false
  },
  SauceType: {
    type: String,
    default: 'dry',
    required: true
  },
  BoxSize: {
    type: String,
    default: null,
    required: true
  },
  EpNo: {
    type: Number,
    default: null,
    required: true
  },
  URL: {
    type: String,
    default: null,
    required: true
  },
  Price: {
    type: String,
    default: null,
    required: true
  },
  Rating: {
    type: String,
    default: null,
    required: false
  },
  Comments: {
    type: String,
    default: null,
    required: false
  }
});

0 个答案:

没有答案