【问题标题】:how to find distinct field of a model upon an and condition in mongoose?如何在猫鼬的条件下找到模型的不同字段?
【发布时间】:2013-10-07 22:32:01
【问题描述】:

我正在寻找满足这两个条件的模型的不同字段的列表。

来自文档:http://mongoosejs.com/docs/api.html#model_Model.distinct

我发现我可以将一组条件作为Model.distinct() 中的第二个参数传递。但是一系列条件似乎是通过“或”操作联系起来的。也就是说,如果任何一个条件满足该字段就会被列出。

但我只想在两个条件都满足的情况下获得该字段。

当我使用时:

Model.distinct(field).and(arrayOfConditions).exec(callback);

然后我得到错误:

TypeError: Cannot call method 'map' of undefined
    at SchemaBoolean.handleArray (/Users/user1/node_modules/mongoose/lib/schema/boolean.js:56:14)

【问题讨论】:

  • 不加评论投反对票..!为什么?

标签: node.js mongodb mongoose


【解决方案1】:

distinct 不会返回功能完整的 Query 对象,因此您可以这样做:

Model.distinct(field, {$and: arrayOfConditions}).exec(callback);

【讨论】:

  • Model.distinct 总是返回一个 Query 对象。 Model.distinct 依次调用 Query.distinct 返回一个 Query 对象。 mongoosejs.com/docs/api.html#query_Query-distinct
  • 但是您的答案效果很好.. 还有这个:Model.distinct(field, {$and : arrayOfConditions }, callback)
猜你喜欢
  • 2016-04-25
  • 2021-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-14
  • 1970-01-01
  • 2015-04-11
  • 2021-04-07
相关资源
最近更新 更多