草庐IT

arrays - $in 需要一个数组作为第二个参数,找到 : missing

coder 2023-10-27 原文

有人可以告诉我我做错了什么吗?

数据库文件结构:

{
    "_id" : "module_settings",
    "moduleChildren" : [
        {
            "_id" : "module_settings_general",
            "name" : "General",
        },
        {
            "_id" : "module_settings_users",
            "name" : "Users",
        },
        {
            "_id" : "module_settings_emails",
            "name" : "Emails",
        }
    ],
    “permissions” : [
        "module_settings_general",
        "module_settings_emails"
    ]
}

流水线阶段:

{ $project: {
    filteredChildren: {
        $filter: {
           input: "$moduleChildren",
           as: "moduleChild",
           cond: { $in : ["$$moduleChild._id", "$permissions"] }
        }
    },
}}

我需要过滤“moduleChildren”数组以仅显示 ID 在“permissions”数组中的模块。我试过“$$ROOT.permissions”和“$$CURRENT.permissions”但它们都不起作用。我总是收到 $in 缺少数组作为参数的错误。当我像这样对数组进行硬编码时它起作用了:cond: { $in : ["$$moduleChild._id", [“module_settings_general”, "module_settings_emails”]] } 所以问题似乎出在数组的传递。 感谢您的任何建议!

最佳答案

第一个选项 --> 使用聚合

因为您集合中的某些文档可能包含也可能不包含 permissions 字段或者类型不等于数组,这就是您收到此错误的原因。

您可以找到 $type字段,如果它不是数组或不存在于您的文档中,则可以使用 $addFields 将其添加为数组和 $cond聚合

db.collection.aggregate([
  { "$addFields": {
    "permissions": {
      "$cond": {
        "if": {
          "$ne": [ { "$type": "$permissions" }, "array" ]
        },
        "then": [],
        "else": "$permissions"
      }
    }
  }},
  { "$project": {
    "filteredChildren": {
      "$filter": {
        "input": "$moduleChildren",
        "as": "moduleChild",
        "cond": {
          "$in": [ "$$moduleChild._id", "$permissions" ]
        }
      }
    }
  }}
])

第二个选项 -->

在您正在使用的任何 GUI 上转到您的 mongo shell 或 robomongo 并运行 这个命令

db.collection.update(
  { "permissions": { "$ne": { "$type": "array" } } },
  { "$set": { "permissions": [] } },
  { "multi": true }
)

关于arrays - $in 需要一个数组作为第二个参数,找到 : missing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51642472/

有关arrays - $in 需要一个数组作为第二个参数,找到 : missing的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  3. ruby - 在 Ruby 中实现 `call_user_func_array` - 2

    我怎样才能完成http://php.net/manual/en/function.call-user-func-array.php在ruby中?所以我可以这样做:classAppdeffoo(a,b)putsa+benddefbarargs=[1,2]App.send(:foo,args)#doesn'tworkApp.send(:foo,args[0],args[1])#doeswork,butdoesnotscaleendend 最佳答案 尝试分解数组App.send(:foo,*args)

  4. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  5. ruby - 多次弹出/移动 ruby​​ 数组 - 2

    我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby​​数组,我们在StackOverflow上找到一

  6. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  7. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  8. ruby-on-rails - Rails 源代码 : initialize hash in a weird way? - 2

    在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has

  9. ruby - 将数组的内容转换为 int - 2

    我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]

  10. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

随机推荐