【问题标题】:How to write find query that search for id in refrence?如何编写在参考中搜索 id 的查找查询?
【发布时间】:2020-01-02 18:13:26
【问题描述】:

我正在使用 Mongoose 插件,它不允许我填充,或进行链式查询。唯一的选择是提供一个find 查询对象。

我有一个 ItemSchema:

{
    name: String,
    category: {
        ref: 'Category',
        type: Schema.Types.ObjectId,
    },
}

从外面,我可以执行该方法:

this.ItemModel.paginate({
    query: {}, // find query options
    ... other options
});

它工作正常。问题是,我希望它只返回特定类别中的某些项目,比如来自 id "5e0cb1b3c53e196514a30787"

我尝试了以下方法,但它不起作用。只返回一个空数组。

query: { 'category': id }
query: { 'category.$id': id }
query: { 'category': { $in: [id] } }

--

我正在使用这个插件https://github.com/mixmaxhq/mongo-cursor-pagination

【问题讨论】:

    标签: mongoose plugins find


    【解决方案1】:

    好的,我发现了。使用ObjectID 有效:

         query: { category: new ObjectID(category) },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      • 2018-12-02
      相关资源
      最近更新 更多