尽管只查询一个,但查找返回数组中所有项目的查询
find query returning all items in array despite querying only on one
我正在编写查询以仅作用于数组中的一项,但查询返回整个数组。
我有一个包含这样数据的集合:
"testScenarioId":"100",
"testSteps":[
{
"edit":false,
"controlId":1450419683150,
"controlName":"scanTextBox",
"objProp":"id->articleSearch_scanViewPopup_field",
"objPropType":15,
"action":{
"id":6
},
"screenId":1450419663920,
"screenName":"Order.Scan",
"applicationId":4,
"applicationName":"GSM",
"testCaseId":"100",
"index":1,
"testStepNumber":"1"
},
{
"edit":false,
"controlId":1450419683894,
"controlName":"scansearchbutton",
"objProp":"id->articleSearch_scanViewPopup_field_HelpIcon",
"objPropType":17,
"action":{
"id":2
},
"screenId":1450419663920,
"screenName":"Order.Scan",
"applicationId":4,
"applicationName":"GSM",
"testCaseId":"100",
"index":2,
"testStepNumber":"2"
}]}
我想根据条件 "testScenarioId" : "100"
和 "testSteps.testStepNumber" : "1"
更新集合;这意味着必须更新 testSteps 数组中的第一个文档,如下所示
{
"edit":false,
"controlId":1450419683150,
"controlName":"scanTextBox",
"objProp":"id->articleSearch_scanViewPopup_field",
"objPropType":15,
"action":{
"id":6
},
"screenId":1450419663920,
"screenName":"Order.Scan",
"applicationId":4,
"applicationName":"GSM",
"testCaseId":"100",
"index":1,
"testStepNumber":"1"
}
但令我惊讶的是,当我 运行 这个命令时:
db.TestSteps.find({"testScenarioId":"100","testSteps.testStepNumber":"1"})
在 mongo shell 中,我得到 所有 testSteps 数组中的文档。
编辑:我想知道 Java 代码,使用上述标准更新数组中的文档。
提前致谢。
尝试在查找查询中添加 $elemMatch,
db.TestSteps.find({"testScenarioId":"100", "testSteps": $elemMatch: {"testStepNumber":"1"}})
您需要添加投影。
db.TestSteps.find( { "testScenarioId":"100"},{"testSteps": { $elemMatch: {"testStepNumber":"1" } } } )
查询文档
您可以在 find
的 projection
参数中使用 $ projection operator。
查找命令的语法是:
find(query, projection)
所以这应该有效:
db.TestSteps.find({"testScenarioId":"100","testSteps.testStepNumber":"1"}, { 'testSteps.$': 1 })
- 请注意,当您指定
projection
时,只会显示选定的字段,因此如果您还需要其他字段 return,请将它们添加到您的投影中。
详细了解投影 here
更新文档
要根据搜索到的数组元素进行更新,您还可以使用 $:
db.TestSteps.update(
{"testScenarioId":"100","testSteps.testStepNumber":"1"},
{$set: {'testSteps.$.new_field': 'hello world'}}
)
$set 中的 $ operator 仅针对查询中匹配的 第一个数组元素。
使用find获取元素,修改,保存回来
您需要:
- 获取包含数组元素的文档
- 仅从文档中提取元素
- 将元素修改成你想要的
- 将其保存回相同位置 - 覆盖原始元素
var doc = db.TestSteps.findOne({"testScenarioId":"100","testSteps.testStepNumber":"1"}, { 'testSteps.$': 1 });
var the_element = doc.testSteps[0];
var updated_element = changeThisElement(the_element);
db.TestSteps.update(
{"testScenarioId":"100","testSteps.testStepNumber":"1"},
{$set: { 'testSteps.$': updated_element }}
)
我正在编写查询以仅作用于数组中的一项,但查询返回整个数组。
我有一个包含这样数据的集合:
"testScenarioId":"100",
"testSteps":[
{
"edit":false,
"controlId":1450419683150,
"controlName":"scanTextBox",
"objProp":"id->articleSearch_scanViewPopup_field",
"objPropType":15,
"action":{
"id":6
},
"screenId":1450419663920,
"screenName":"Order.Scan",
"applicationId":4,
"applicationName":"GSM",
"testCaseId":"100",
"index":1,
"testStepNumber":"1"
},
{
"edit":false,
"controlId":1450419683894,
"controlName":"scansearchbutton",
"objProp":"id->articleSearch_scanViewPopup_field_HelpIcon",
"objPropType":17,
"action":{
"id":2
},
"screenId":1450419663920,
"screenName":"Order.Scan",
"applicationId":4,
"applicationName":"GSM",
"testCaseId":"100",
"index":2,
"testStepNumber":"2"
}]}
我想根据条件 "testScenarioId" : "100"
和 "testSteps.testStepNumber" : "1"
更新集合;这意味着必须更新 testSteps 数组中的第一个文档,如下所示
{
"edit":false,
"controlId":1450419683150,
"controlName":"scanTextBox",
"objProp":"id->articleSearch_scanViewPopup_field",
"objPropType":15,
"action":{
"id":6
},
"screenId":1450419663920,
"screenName":"Order.Scan",
"applicationId":4,
"applicationName":"GSM",
"testCaseId":"100",
"index":1,
"testStepNumber":"1"
}
但令我惊讶的是,当我 运行 这个命令时:
db.TestSteps.find({"testScenarioId":"100","testSteps.testStepNumber":"1"})
在 mongo shell 中,我得到 所有 testSteps 数组中的文档。
编辑:我想知道 Java 代码,使用上述标准更新数组中的文档。
提前致谢。
尝试在查找查询中添加 $elemMatch,
db.TestSteps.find({"testScenarioId":"100", "testSteps": $elemMatch: {"testStepNumber":"1"}})
您需要添加投影。
db.TestSteps.find( { "testScenarioId":"100"},{"testSteps": { $elemMatch: {"testStepNumber":"1" } } } )
查询文档
您可以在 find
的 projection
参数中使用 $ projection operator。
查找命令的语法是:
find(query, projection)
所以这应该有效:
db.TestSteps.find({"testScenarioId":"100","testSteps.testStepNumber":"1"}, { 'testSteps.$': 1 })
- 请注意,当您指定
projection
时,只会显示选定的字段,因此如果您还需要其他字段 return,请将它们添加到您的投影中。 详细了解投影 here
更新文档
要根据搜索到的数组元素进行更新,您还可以使用 $:
db.TestSteps.update(
{"testScenarioId":"100","testSteps.testStepNumber":"1"},
{$set: {'testSteps.$.new_field': 'hello world'}}
)
$set 中的 $ operator 仅针对查询中匹配的 第一个数组元素。
使用find获取元素,修改,保存回来
您需要:
- 获取包含数组元素的文档
- 仅从文档中提取元素
- 将元素修改成你想要的
- 将其保存回相同位置 - 覆盖原始元素
var doc = db.TestSteps.findOne({"testScenarioId":"100","testSteps.testStepNumber":"1"}, { 'testSteps.$': 1 });
var the_element = doc.testSteps[0];
var updated_element = changeThisElement(the_element);
db.TestSteps.update(
{"testScenarioId":"100","testSteps.testStepNumber":"1"},
{$set: { 'testSteps.$': updated_element }}
)