首页 > 解决方案 > 如何将 SQL 语法转换为 Mongo?

问题描述

我使用 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

标签: ruby-on-railsmongoid

解决方案


找到了解决方案:

@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)

推荐阅读