简体   繁体   English

在mongodb php查询中使用$操作

[英]Use of $ operate in mongodb php query

From MongoDB Documentation at http://docs.mongodb.org/manual/reference/operator/update/positional/ one can use $ operator to update inner field, like for given example http://docs.mongodb.org/manual/reference/operator/update/positional/的 MongoDB文档中,可以使用$运算符来更新内部字段,如给定的示例

{ "_id" : 4, "grades" : [ { grade: 80, mean: 75, std: 8 },
                              { grade: 85, mean: 90, std: 5 },
                              { grade: 90, mean: 85, std: 3 } ] }

Use the positional $ operator to update the value of the std field in the embedded document with the grade of 85: 使用位置$运算符将等级为85的嵌入文档中的std字段值更新:

db.students.update( { _id: 4, "grades.grade": 85 }, { $set: { "grades.$.std" : 6 } } )

Can somebody please explain me how $ is playing a role? 有人可以解释一下$如何发挥作用吗? Why MongoDB developers didn't make it simpler, like we can find document with "grades.grade":85 and also update with "grades.std":6 ? 为什么MongoDB开发人员没有简化它,例如我们可以找到带有"grades.grade":85文档,也可以使用"grades.std":6更新? Or I am missing something? 还是我想念什么?

The positional $ operator acts as a placeholder for the first element that matches the query document. 位置$运算符充当与查询文档匹配的第一个元素的占位符。 Without $ , you would have to perform extra steps to get the index of the matched element. 没有$ ,您将不得不执行额外的步骤来获取匹配元素的索引。 Otherwise the index would be hardcoded. 否则,索引将被硬编码。

Update example: 更新示例:

db.students.update( { _id: 4, "grades.grade": 85 }, { $set: { "grades.$.std" : 6 })

"grades.grade":85 is find a document where 'grades' has 'grade' set to `85 . "grades.grade":85find a document where 'grades' has 'grade' set to `85

"grades.2.std":6 reads as within 'grades', set 'std' to 6 for the element at index 2 . "grades.2.std":6 within 'grades', set 'std' to 6 for the element at index 2读取within 'grades', set 'std' to 6 for the element at index 2

"grades.$.std:6" reads as within 'grades', set 'std' to 6 for the first matched element . "grades.$.std:6"读为within 'grades', set 'std' to 6 for the first matched element

"grades.std":6 sounds like within 'grades', for all elements set 'std' to '6' . within 'grades', for all elements set 'std' to '6' "grades.std":6听起来像是within 'grades', for all elements set 'std' to '6' In MongoDB 2.6, this isn't a supported feature . 在MongoDB 2.6中,这不是受支持的功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM