首页 > 解决方案 > 如何在 mongodb 中使用文档值长度进行搜索查询

问题描述

在我的收藏中,我有组,他们可以限制其中的成员。我想查询所有在搜索表单中给出的数字留下足够空间的组。我为我的 API 使用猫鼬和节点。

因此,当一个组中有 2 个成员时,限制为 3 并且用户搜索组中剩下的 2 个位置。该组不会显示,但会显示其他组。

我该如何查询这个?我已经找到了类似的东西,query.$expr = { $gt: [{ $strLenCP: "$members" }, params.searchValue /*2 in my example*/ ] };但我真的不明白。

这是我的猫鼬模式:

let GroupSchema = new Schema({
  name: {
    type: String,
    required: true
  },
  status: {
    type: String,
    required: true
  },
  game: {
    type: Schema.Types.ObjectId,
    ref: 'Games',
    required: true
  },
  alias: {
    type: String,
    required: true,
    unique: true
  },
  owner: {
    type: Schema.Types.ObjectId,
    ref: 'User'
  },
  range: Number,
  image: String,
  description: String,
  members: [{
    type: Schema.Types.ObjectId,
    ref: 'User'
  }],
  updated: Date,
  created: Date
});

标签: node.jsmongodbmongooseschema

解决方案


推荐阅读