【问题标题】:How to convert SQL syntax into Mongo?如何将 SQL 语法转换为 Mongo?
【发布时间】:2020-08-29 06:59:34
【问题描述】:

我使用 Ruby on Rails 和 Mongoid

如何转换此语法以使其与 Mongo 一起使用?

def index
  @conversations = Conversation.where("sender_id = ? OR receiver_id = ?", current_user.id, current_user.id)
end

def index
  @conversation.messages.where("user_id != ? AND read = ?", current_user.id, false).update_all(read: true)
end

【问题讨论】:

  • 基本上你是在问如何在非 sql 数据库上运行 sql 查询。从根本上说,它们的工作方式不同,因此很难拥有转换器。

标签: ruby-on-rails mongoid


【解决方案1】:

找到了解决办法:

@conversations = Conversation.where("sender_id = ? OR receiver_id = ?", current_user.id, current_user.id)

改为

@conversations = Conversation.any_of({sender_id: current_user.id}, {receiver_id: current_user.id})

第二行必须完全重写,不能使用。所以不知道行不行

@conversation.messages.where("user_id != ? AND read = ?", current_user.id, false).update_all(read: true)

可能不起作用

@conversation.messages.not.where(user_id: current_user.id).where(read: false).update_all(read: true)

【讨论】:

  • 第二行应该查找 user_id 不是当前用户的消息?
猜你喜欢
  • 1970-01-01
  • 2017-09-14
  • 1970-01-01
  • 1970-01-01
  • 2021-06-29
  • 1970-01-01
  • 2013-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多