簡體   English   中英

Rails + MongoDB - 查詢Mongoid多值哈希字段

[英]Rails + MongoDB - Query on Mongoid multi value Hash field

我使用的是Rails 3.2.8,Mongoid 3.1.4和Ruby 1.9.3

我有簡單的JSON:

{
    _id: ObjectId("51f65be999c655122d0097ed"),
    association_chain: [
        {
            name: "VendorForecast",
            id: ObjectId("51f65be999c655122d0097ec")
        }
    ],
    scope: "vendor_forecast",
    original: {},
    modified: {
        category_id: ObjectId("51f65b3799c655122d00008e"),
        scenario_id: ObjectId("51f65b3799c655122d00002e"),
        period_id: ObjectId("51bf800b99c655fe13000152"),
        value: 150000,
        company_id: ObjectId("51f65b3699c655122d000002")
    },
    version: 1,
    action: "create",
    modifier_id: ObjectId("51f65b3799c655122d000006"),
    updated_at: ISODate("2013-07-29T12:11:21+00:00"),
    created_at: ISODate("2013-07-29T12:11:21+00:00")
}

我想得到所有的模型

modified: {
  company_id: ObjectId("51f65b3699c655122d000002")
}

如果我只是簡單地查詢:

1.9.3p429 :026 > HistoryTracker.where(modified: {"company_id" => "51f654a099c655032a000002"}).size
 => 0 

我沒有結果。

有沒有想過如何讓這個工作?

嘗試:

HistoryTracker.where("modified.company_id" => Moped::BSON::ObjectId("51f654a099c655032a000002")}).count

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM