-2
{
"_id":ObjectId("570404d3b4aefafb2d975e49"),
"timeStamp":NumberLong(1459881168860),
"activityMessages":null,
"oldLocation":null,
"changeType":"ContainerCreated",
"userId":"naveen",
"newLocation":"L1-500",
"itemHistory":[
{
"userId":null,
"timeStamp":null,
"oldLocation":null,
"newLocation":null,
"changeType":"ItemAdded",
"activityMessages":null,
"itemNumber":325136,
"purchCompanyId":1003,
"poNum":"100",
"oldQty":NumberLong(0),
"newQty":NumberLong(100),
"inventoryStatus":"Received"
},
{
"userId":null,
"timeStamp":null,
"oldLocation":null,
"newLocation":null,
"changeType":"ItemAdded",
"activityMessages":null,
"itemNumber":325136,
"purchCompanyId":1003,
"poNum":"100",
"oldQty":NumberLong(0),
"newQty":NumberLong(2000),
"inventoryStatus":"Frozen"
},
{
"userId":null,
"timeStamp":null,
"oldLocation":null,
"newLocation":null,
"changeType":"ItemAdded",
"activityMessages":null,
"itemNumber":88888,
"purchCompanyId":1003,
"poNum":"101",
"oldQty":NumberLong(0),
"newQty":NumberLong(200),
"inventoryStatus":"Claims"
}
}
这是我典型的Mongo文档。在查询给定时间戳范围内的给定项目编号时使用以下查询:无法正确读取文件内的数组的mongo结果
{ "timeStamp" : { "$gte" : 0 , "$lte" : 1459933945543} , "itemHistory.itemNumber" : 325136}
我收到了正确的答复!然而,在搜索itemNumber 88888时,我没有得到任何相同的查询响应。
{ "timeStamp" : { "$gte" : 0 , "$lte" : 1459933945543} , "itemHistory.itemNumber" : 88888}
下面的查询工作得很好:
{ "timeStamp" : { "$gte" : 0 , "$lte" : 1459933945543} , "itemHistory" : {"$elemMatch":{"itemNumber":112}}}
响应:
Fetched 0 records
这使我想知道如果只有一个文档内的数组的第一个元素被拾起同时匹配查询条件。我尝试了一堆其他查询,似乎没有任何工作。
我需要返回包含给定itemNumber的整个文档!
TIA
这是你真正的代码有保证的错字。再次检查。这不是一个错误或类似的东西。你只需输入一个字段名称或集合或不正确的东西。无法重现。这里给出的第二个查询匹配文件就像它应该。 –
{“timeStamp”:{“$ gte”:0,“$ lte”:1459933945543},“itemHistory”:{“$ elemMatch”:{“itemNumber”:112}}}此查询有效。没有错字。 – Tanvi
'{“timeStamp”:{“$ gte”:0,“$ lte”:1459933945543},“itemHistory.itemNumber”:88888}'完全适用于您在此问题中提供的数据。我知道是因为我将文档复制到一个集合中,然后运行查询。当你需要匹配数组中的“两个或更多条件”时,你只需要**需要'$ elemMatch'。这是它的用法,没有别的。 –