我正在编写一个查询以仅处理数组中的一个项目,但查询正在返回整个数组。发现查询返回数组中的所有项目,尽管只查询一个
我有这样的数据的集合:
"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代码,以更新使用上述条件的阵列中的一文件。
在此先感谢。
喜非难和欢迎堆栈溢出。请记住,find查询中的条件仅确定集合中的哪些文档被返回;它们不会影响返回中包含哪些子文档(在数组中)。 要从数组中返回一个项目,您需要查看聚合查询中的[unwind operator](https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/)。 –