Php 如何更新mongodb文档
嗨,我真的是mongodb新手 我有这样一份文件:Php 如何更新mongodb文档,php,mongodb,updates,Php,Mongodb,Updates,嗨,我真的是mongodb新手 我有这样一份文件: { "_id": ObjectId("53182e32e4b0feedb1dea751"), "solutions": [ [ { "solution": "Double Room Economy (Without Breakfast)", "board": "Room Only", "id":
{
"_id": ObjectId("53182e32e4b0feedb1dea751"),
"solutions": [
[
{
"solution": "Double Room Economy (Without Breakfast)",
"board": "Room Only",
"id": "HK-15501871",
"price": 5000,
"available": "1",
"CXL": "[]",
"unique": 0
},
{
"solution": "Double Room Economy (With Breakfast)",
"board": "Room Only",
"id": "HK-15501871",
"price": 4600,
"available": "1",
"CXL": "[]",
"unique": 1
},
{
"solution": "Double Room Economy (Room Only)",
"board": "Room Only",
"id": "HK-15501871",
"price": 5500,
"available": "1",
"CXL": "[]",
"unique": 2
}
]
]
}
update({
_id: ObjectId("53182e32e4b0feedb1dea751"),
solutions.id: HK-15501871,
solutions.CLX: "Whatever!",")
},{
$set: {"comments.$.type": abc}
}, false, true
);
我需要更新第二个解决方案数组中的CXL
字段。
so解决方案1.CXL
以下是我获取文档的方式:
$collection = $this->getCollection();
$query = array("_id"=>new MongoId($id));
$document = $collection->findOne($query);
现在我需要在不接触其他字段的情况下更新该字段
我该怎么办
谢谢
多亏了@Sammaye
我这样解决:
$collection->update(
array('_id' => new MongoId('..')),
array('$set' => array('solutions.0.1.CXL' => 'something'))
);
您可以这样更新:
{
"_id": ObjectId("53182e32e4b0feedb1dea751"),
"solutions": [
[
{
"solution": "Double Room Economy (Without Breakfast)",
"board": "Room Only",
"id": "HK-15501871",
"price": 5000,
"available": "1",
"CXL": "[]",
"unique": 0
},
{
"solution": "Double Room Economy (With Breakfast)",
"board": "Room Only",
"id": "HK-15501871",
"price": 4600,
"available": "1",
"CXL": "[]",
"unique": 1
},
{
"solution": "Double Room Economy (Room Only)",
"board": "Room Only",
"id": "HK-15501871",
"price": 5500,
"available": "1",
"CXL": "[]",
"unique": 2
}
]
]
}
update({
_id: ObjectId("53182e32e4b0feedb1dea751"),
solutions.id: HK-15501871,
solutions.CLX: "Whatever!",")
},{
$set: {"comments.$.type": abc}
}, false, true
);
你可能想经历一次
编辑
要通过第一个索引进行实际更新,可以执行以下操作:
$db->collection->update(
['_id' => new \MongoId($id)],
['$set' => ['solutions.0.1.CLX' => 'whatever']]
);
我在发布以下信息时误读了问题:
那么,您想要更新文档中的所有CXL字段(因为您只按顶级文档\u id
进行搜索)是什么呢
如果不手动拉出此文档并迭代解决方案
字段中的子文档,然后重新保存它,这是不可能的
这是因为目前没有办法说“更新所有匹配项”
然而,这很可能就是您想要寻找的JIRA:只要您知道,您将更新第二个元素,然后使用数组的索引进行更新。但接下来就是这个问题。首先,您需要$set运算符,以避免文档被吹走,只需设置字段值:
db.collection.update(
{u id:ObjectId(“53182e32e4b0feedb1dea751”),
{$set:{“solutions.0.1.CXL”:[1,2,3]}
)
如果您只想将添加到数组中,而不是替换整个数组,那么只需使用$push即可:
db.collection.update(
{u id:ObjectId(“53182e32e4b0feedb1dea751”),
{$push:{“solutions.0.1.CXL”:4}
)
如果您注意符号,那么您会注意到数组索引值出现在要更新的字段中。这有一个很好的理由,可以在操作员的文档中阅读
问题是您有一个嵌套数组,如文档所述,如果您试图匹配该嵌套数组中的项,则会导致问题。问题是,如果您尝试使用“位置”操作符来查找查询中查找的内容的匹配索引,那么它将包含它找到的第一个数组索引匹配的值
在这种情况下,这将是您的“顶级”数组,“找到的”索引是0
,而不是您可能期望的1
如果要使用嵌套数组,请注意此问题 不,我不想更新所有CXL字段,只想更新一个特定字段。在这个例子中,我想用索引更新对象中的CXL1@JackTurky同意。这个问题被误解了。为了您的利益,我随附了一个答案。@JackTurky CLX是一个字符串而不是数组,您认为它是数组吗?对不起。。使用$collection->update(数组(''u id'=>newmongoid('..))、数组('$set'=>array('solutions.0.1.CLX'=>something'));它编辑所有文档在解决方案[0][1][CXL]=“某物”中更改它删除我所有其他fields@JackTurky嗯,我必须测试一下,不应该是这样的,我自己使用这个查询位置$操作符在这里不起作用,因为匹配的数组索引将是顶级数组的索引。在这种情况下,它将是0
,需要的索引是1
。我提交的答案以及文档链接解释了原因。