我知道Mongoid4还在测试中,也许我找到了一个bug,但是我很难理解为什么第一个查询工作,而第二个查询什么都不返回:
Product.or({sender_uid: params[:user_id]}, {receiver_uid: params[:user_id]})
Product.where({sender_uid: params[:user_id]}).or({receiver_uid: params[:user_id]})这使得很难组合任何复杂的查询,因此任何指针都会受到欢迎。
发布于 2014-03-27 15:27:51
请参见以下示例:
Product 1: sender_uid = 1, receiver_uid = 2
Product 2: sender_uid = 2, receiver_uid = 1
Product 3: sender_uid = 1, receiver_uid = 2
params[:user_id] = 1在第一个查询中,您得到的是ALL,sender_uid 或 receiver_uid等于1的产品。那是产品1,2和3。
在第二个查询中,您将查询sender_uid为1的所有产品。这是Product1和Product3,然后(根据这个标准),产品的receiver_id = 1。产品1,而不是产品2都没有uid 1的接收器。所以,这就是为什么你什么也得不到。在第二个查询中所做的事情如下:
Product.where(sender_uid: params[:user_id]).where(receiver_uid: params[:user_id])更新:
对评论的答复:
Product.or({ product_id: 1 }, { product_id: 2, sender_uid: 2 })如您所见,or方法接收到条件的散列。每个查询都像一个where查询。
https://stackoverflow.com/questions/22512738
复制相似问题