无法正确获取文档中的数组的mongo结果

时间:2016-04-06 11:56:09

标签: mongodb

{  
   "_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文档。在使用查询查询给定时间戳范围内的给定项目编号时:

{ "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

1 个答案:

答案 0 :(得分:0)

我使用和不使用elemMatch执行了两种类型的查询,并且都获得了正确的结果。

db.collection.find({
   "timeStamp" :  { "$gte" : 0 , "$lte" : 1459933945543} , 
   "itemHistory.itemNumber" : 88888
})

db.collection.find({
  "timeStamp" :  { "$gte" : 0 , "$lte" : 1459933945543} , 
  "itemHistory" :{$elemMatch:{itemNumber: 88888}}
})

结果(为简洁起见,省略了完整文件):

{ 
    "_id" : ObjectId("570500508eb3259244949d82"), 
    "timeStamp" : 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" : 88888.0, 
            "purchCompanyId" : 1003.0, 
            "poNum" : "101", 
            "oldQty" : 0, 
            "newQty" : 200, 
            "inventoryStatus" : "Claims"
        },
        ...
    ]
}