当更新Mongo数据库时,我想更新多个嵌套数组中的文档。但是,尝试更新它时,我的查询都无效。以下是现有表格的表格:
Array
(
[0] => Array
(
[_id] => MongoId Object
(
[$id] => 5ce268ef6803fa8f237b23c6
)
[member_number] => 26b091bdd3f9505b2103a187f3ac6643
[member_forename] => 1d40521662cc484d7c06992c61303ba6
[member_surname] => 581c78bf6fe11cc0ff53a805a100d441
[certificates] => Array
(
[0] => Array
(
[member_id] => MongoId Object
(
[$id] => 5ce268ef6803fa8f237b23c6
)
[certificate_id] => MongoId Object
(
[$id] => 5ce4dd3d6803fa15454cbed5
)
[skills_completed] => N
[certificate_issued] => N
[details] => Array
(
[0] => Array
(
[member_id] => MongoId Object
(
[$id] => 5ce268ef6803fa8f237b23c6
)
[session_id] => MongoId Object
(
[$id] => 5ce4f1a96803fa574e4cbede
)
[mark_status] => A
[cd_status] => A
[gradeId] => MongoId Object
(
[$id] => 5ca48ccfa36f74db9a8a0620
)
[gradeKey] => NP
[achieved_date] => MongoDate Object
(
[sec] => 1558531565
[usec] => 713000
)
[detail_id] => MongoId Object
(
[$id] => 5ce4dd526803fa1f454cbed7
)
[marked_by] => MongoId Object
(
[$id] => 5cb5622a6803fa640b7b23c7
)
)
)
[certificate_issued_by] =>
[certificate_session_id] =>
[sc_date] =>
)
[updated_at] => MongoDate Object
(
[sec] => 1558341930
[usec] => 482000
)
)
)
我想更新嵌套在证书中的详细信息部分之一。证书数组在成员表中。
我的匹配条件是_id =成员ID, certificate.certificate_id = xxxx,
certificates.details.detail_id = xxxx
我有完整的数组,它将替换明细数组的第零个位置。如何做到这一点?
答案 0 :(得分:0)
假设涉及嵌入式关联,让Mongoid进行工作应该很简单:
class Foo
include Mongoid::Document
embeds_many :bars
end
class Bar
include Mongoid::Document
has_one :thing
embedded_in :foo
end
class Thing
include Mongoid::Document
belongs_to :bar
end
foo = Foo.new
foo.bars = [Bar.new, Bar.new]
foo.save!
foo.bars[0].thing = Thing.new
foo.save!
foo.reload
foo.bars.map(&:thing)
# => [#<Thing _id: 5ce85f47026d7c20c4c0613a, bar_id: BSON::ObjectId('5ce85f47026d7c20c4c06138')>, nil]
请注意https://jira.mongodb.org/browse/MONGOID-2951-在上面的示例中,我更改了Bar实例之一的字段,但未修改Bar的列表。